Advanced Node and Express - Passport Strategies - test for POST request to /login fails but code works as intended

I think that there is a bug in this test case. Whether it is a bug in the test or a problem in my code, I would like some help getting past this exercise.

While I think my code is behaving correctly at this point on Glitch, I cannot get the FCC test to pass. The first test, “All steps should be correctly implemented in the server.js”, passes consistently. But, “A POST request to /login should correctly redirect to /” still fails.On 5/14/2020 it was timing out. Today, 5/15/2020, that request is returning a 200 status but, confusingly, the test itself still says it failed. My POST worked as expected testing it out in Postman, it redirected to “/” and rendered index.pug – and my console logged the username that I tried in Postman. So, I’d like to know what I can do to get the FCC test to pass?

My code so far
I have even tried using the code from Joseph Livengood’s gist, which was linked in the challenge description. I had to make a couple changes because I’m using a newer mongodb package. I also tried rolling back to an older mongodb so that the gist code as written would work.

'use strict';

const express     = require('express');
const bodyParser  = require('body-parser');
const fccTesting  = require('./freeCodeCamp/fcctesting.js');
const session     = require('express-session');
const passport    = require('passport');
const mongo       = require('mongodb').MongoClient;
const ObjectID    = require('mongodb').ObjectID;
const LocalStrategy = require('passport-local');

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

app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,
}));
app.use(passport.initialize());
app.use(passport.session());

mongo.connect(process.env.DATABASE, (err, db) => {
    if(err) {
        console.log('Database error: ' + err);
    } else {
        console.log('Successful database connection');

        passport.serializeUser((user, done) => {
          done(null, user._id);
        });

        passport.deserializeUser( (id, done) => {
            db.collection('users').findOne(
                {_id: new ObjectID(id)},
                (err, doc) => {
                    done(null, doc);
                }
            );
        });
      
        passport.use(new LocalStrategy(
          function(username, password, done) {
            db.collection('users').findOne({ username: username }, function (err, user) {
              console.log('User '+ username +' attempted to log in.');
              if (err) { return done(err); }
              if (!user) { return done(null, false); }
              if (password !== user.password) { return done(null, false); }
              return done(null, user);
            });
          }
        ));

        app.route('/')
          .get((req, res, next) => {
            res.render(process.cwd() + '/views/pug/index', {title: 'Home Page', message: 'login', showLogin: true, showRegistration: true});
          });
      
        app.route('/login')
          .post(passport.authenticate('local', { failureRedirect: '/' }),(req,res,next) => {
               res.redirect('/profile');
          });
      
        app.route('/profile')
          .get((req,res, next) => {
               res.render(process.cwd() + '/views/pug/profile');
          });

        app.listen(process.env.PORT || 3000, () => {
          console.log("Listening on port " + process.env.PORT);
        });  
}});

My original MongoDB database was called “test”. I created a new one called “fcc-adv-node-express”. I think I have the above code correct and that it ought to pass the tests regardless of which of the two databases I use (both have a collection called “users”).

When I use the page manually I can see “User attempted to log in.” And, it does redirect back to ‘/’ which then renders index.pug as expected.

But when I run the test on the FCC page all that logs to the console is “requested” which is coming from the fcc-testing.js. So it doesn’t get to the code where “User attempted to log in” would be logged, nor does it get around the redirected GET that should just request the index page. I suspected it may not even hit the ‘/login’ endpoint. But if I look at developer tools on the FCC page, I can see a POST to /login, which gets a 302 response, followed by a GET request initiated by /login. And even when the GET request shows a status of 200, the test itself still fails.

Is something wrong with my code (it behaves for me in Glitch so I don’t think so?) or is something wrong with the test? What else can I try?

browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: How to Use Passport Strategies

Link to the challenge:

I’ve looking at chrome developer tools as well. Here is what I see on the network tab when I submit my url to the fcc test.


So, the POST to “/login” looks good to me, the status is a 302 and it appears to get redirected. But, once redirected to my url it just sits there at (pending) forever, and I don’t understand why.

If I open dev tools on a new tab and manually paste my url into the address bar, it comes right up. if I enter some name and password and press Login, it redirects as expected and is not slow.

Are you able to pass this case finally?

Hello there,

I realise this post went under our radar, but for others (@simenziao) searching:

  • I suggest you open your own topics, with your own links to your project.
  • The test is looking for:
assert.match(data, /Looks like this page is being rendered from Pug into HTML!/gi)

All that means is the page with the text Looks like this page is being rendered from Pug into HTML! should be present on the /login route.