Implementation of Social Authentication Route auth/github/callback should accept a get request

Advanced Node and Express - Implementation of Social Authentication II

Hi,
After few days and a very long debugging I can successful login and log out with github.
Please try to log in and feedback if any error occured.

If on pubic github profile you leave empty the name field, you give back John Doe, as login name because we use name: profile.displayName || "John Doe", instead of name: profile.displayName || profile.username,

On github profile If settings >> emails >> Keep my email addresses private checkbox ticked, you can get an error with the freeCodeCamp solution:

TypeError: Cannot read property '0' of undefined
    at Strategy._verify (/app/server.js:  email: profile.emails[0].value || "No public email",

and your code stop running.
I resolved this issue with the following code:

let email;
profile.emails ? email = profile.emails[0].value : email = "No public email";

… and in the insert Object:

email: email,

My full solution:

'use strict';

const express     = require('express');
const bodyParser  = require('body-parser');
const fccTesting  = require('./freeCodeCamp/fcctesting.js');
const session     = require('express-session');
const mongo       = require('mongodb').MongoClient;
const passport    = require('passport');
const GitHubStrategy = require("passport-github").Strategy;

const app = express();

fccTesting(app); //For FCC testing purposes

app.use('/public', express.static(process.cwd() + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.set('view engine', 'pug')

mongo.connect(process.env.DATABASE, (err, db) => {
    if(err) {
        console.log('23. Database error: ' + err);
    } else {
        console.log('25. Successful database connection');
        const dbo = db.db("cluster0-l7ww6");
        
        app.use(session({
          secret: process.env.SESSION_SECRET,
          resave: true,
          saveUninitialized: true,
        }));
        app.use(passport.initialize());
        app.use(passport.session());
      
        function ensureAuthenticated(req, res, next) {
          if (req.isAuthenticated()) {
              return next();
          }
          res.redirect('/');
        };

        passport.serializeUser((user, done) => {
          console.log("44. serialisation and app listen...", user);
          done(null, user.id);
        });

        passport.deserializeUser((id, done) => {
            dbo.collection('socialusers').findOne(
                {id: id},
                (err, doc) => {
                    console.log("52. deserialize user", id);
                    done(null, doc);
                }
            );
        });

      
        /*
        *  ADD YOUR CODE BELOW
        */
      
      passport.use(new GitHubStrategy({
        clientID: process.env.GITHUB_CLIENT_ID,
        clientSecret: process.env.GITHUB_CLIENT_SECRET,
        callbackURL: "https://wide-wrist.glitch.me/auth/github/callback"
      }, function(accessToken, refreshToken, profile, cb) {
        console.log("profile: ", profile);
        let email;
        profile.emails ? email = profile.emails[0].value : email = "No public email";
        dbo.collection("socialusers").findAndModify({
          id: profile.id
        }, {}, {
          $setOnInsert: {
            id: profile.id,
            name: profile.displayName || "John Doe",
            photo: profile.photos[0].value || "",
            email: email,
            created_on: new Date(),
            provider: profile.provider || ""
          }, $set: {
            last_login: new Date()
          }, $inc: {
            login_count: 1
          }}, {
          upsert: true,
          new: true
        }, (err, doc) => {
          console.log("github strategy", doc);
          return cb(null, doc.value);
        });
      }));
      
      app.route("/auth/github")
        .get(passport.authenticate("github"))
      ;
      
      app.route("auth/github/callback")
        .get(passport.authenticate("github", {failureRedirect: "/"}), (req, res) => {
          console.log("76. req is:", req) ;
          res.redirect("/profile");
        })
      ;
      
        app.route('/auth/github')
          .get(passport.authenticate('github'));
      
        app.route('/auth/github/callback')
          .get(passport.authenticate('github', { failureRedirect: '/' }), (req,res) => {
              res.redirect('/profile');
          });
      
              
      
      
      
        /*
        *  ADD YOUR CODE ABOVE
        */
      
      
        app.route('/')
          .get((req, res) => {
            res.render(process.cwd() + '/views/pug/index');
          });

        app.route('/profile')
          .get(ensureAuthenticated, (req, res) => {
              res.render(process.cwd() + "/views/pug/profile", {user: req.user});
          });

        app.route('/logout')
          .get((req, res) => {
              req.logout();
              res.redirect('/');
          });

        app.use((req, res, next) => {
          res.status(404)
            .type('text')
            .send('Not Found');
        });
      
        app.listen(process.env.PORT || 3000, () => {
          console.log("Listening on port " + process.env.PORT);
        });  
}});