Advanced Node and Express - Implementation of Social Authentication

Hi, I’m getting an error while manually running my code here. Any help would be appreciated. I just updated the packages file. I am passing the test for ‘/auth/github’, but failing ‘/auth/github/callback’. Here is my code in server.js:

'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 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, {useNewUrlParser: true},(err, client) => {
    if(err) {
        console.log('Database error: ' + err);
    } else {
        console.log('Successful database connection');
      
      var db = client.db('mytestingdb');
      
        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) => {
          done(null, user.id);
        });

        passport.deserializeUser((id, done) => {
            db.collection('socialusers').findOne(
                {id: id},
                (err, doc) => {
                    done(null, doc);
                }
            );
        });

      
        /*
        *  ADD YOUR CODE BELOW
        */
      
        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.pug');
          });

        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);
        });  
}});

Why did you put app configurations into the callback of mongo.connect?

That was already there. I just clicked on the new glitch project, so the only thing I actually changed in this code was the var db = client.db (because apparently the updated version of mongodb requires that), and then I added the code

        app.route('/auth/github')
          .get(passport.authenticate('github'));

        app.route('/auth/github/callback')
          .get(passport.authenticate('github', {failureRedirect: '/'}),
            (req, res) => {
              res.redirect('/profile');
            });

The first route passes the tests but the GET ‘/auth/github/callback’ is failing.

When I do it manually, the browser sends these errors:

TypeError: Cannot read property '0' of undefined
    at Strategy._verify (/app/server.js:75:36)
    at /rbd/pnpm-volume/6f559774-4f0c-4b20-956a-7406fa06f86a/node_modules/.registry.npmjs.org/passport-oauth2/1.4.0/node_modules/passport-oauth2/lib/strategy.js:193:24
    at /rbd/pnpm-volume/6f559774-4f0c-4b20-956a-7406fa06f86a/node_modules/.registry.npmjs.org/passport-github/1.1.0/node_modules/passport-github/lib/strategy.js:174:7
    at passBackControl (/rbd/pnpm-volume/6f559774-4f0c-4b20-956a-7406fa06f86a/node_modules/.registry.npmjs.org/oauth/0.9.15/node_modules/oauth/lib/oauth2.js:134:9)
    at IncomingMessage.<anonymous> (/rbd/pnpm-volume/6f559774-4f0c-4b20-956a-7406fa06f86a/node_modules/.registry.npmjs.org/oauth/0.9.15/node_modules/oauth/lib/oauth2.js:157:7)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:139:11)
    at process._tickCallback (internal/process/next_tick.js:181:9)

OK I just renamed my project and it passed the tests. I have no idea what happened, but I guess the problem was not in my code.

The FCC test is wildly unpredictable, but at the end of the day, it’s just a low-level, by-the-book test. As long as your code works as it should be, I’m not bothered by failing the test.