Advanced Node and Express - Registration of New Users - I am losing my mind!

I am working on this challenge and I am losing my mind. I have attempted every hack and fix possible - and I still have not passed all tests. Failure is happening with the req.isAuthenticated() function that is placed in the profile page route protection middleware. Cutting the function out gets all but the “Registering should work” test to pass.

My code:

'use strict';

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

const app = express();


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

fccTesting(app); //For FCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.use(bodyParser.urlencoded({ extended: true }));

// Setup Session

  resave: true,
  saveUninitialized: true


// if (process.env.ENABLE_DELAYS) app.use((req, res, next) => {
//   switch (req.method) {
//     case 'GET':
//       switch (req.url) {
//         case '/logout': return setTimeout(() => next(), 500);
//         case '/profile': return setTimeout(() => next(), 700);
//         default: next();
//       }
//     break;
//     case 'POST':
//       switch (req.url) {
//         case '/login': return setTimeout(() => next(), 900);
//         default: next();
//       }
//     break;
//     default: next();
//   }
// });

mongo.connect(process.env.DB, { useNewUrlParser: true }, (err, client) => {
  if (err) {console.log('Danger, Will Robinson!  ' + err)}
  else {
    console.log('DB Connected!')
    const db = client.db()
        passport.serializeUser((user, done) => {
          console.log('Passport Serialize Method: User serialized')
    done(null, user._id)
      passport.deserializeUser((id, done) => {
    {_id: new ObjectId(id)},
    (err, doc) => {
      console.log('Passport Deserialize Method: User deserialized')
    done(null, doc)
    passport.use(new LocalStrategy((username, password, done) => {
      db.collection('users').findOne( {username: username }, 
        (err, user) => {
        console.log('Passport LocalStrategy: User ' + username + ' attempted to log in.');
        if (err) { return done(err) 
                 } else if (!user) { 
                   console.log('Passport LocalStrategy: No user found')
                   return done(null, false) 
                  } else if (password != user.password) { 
                    console.log('Passport LocalStrategy: Incorrect Password')
                    return done(null, false) 
                    } else {
                      console.log('Passport LocalStrategy: User logged in')
                      return done(null, user)
    let ensureAuthenticated = (req, res, next) => {
      if(req.isAuthenticated()) {
        console.log('ensureAuthenicated Function: User is registered, continuing routing')
        return next()
      console.log('ensureAuthenicated Function: User is not registered, redirecting to homepage')
  .get((req, res) => {
    res.render(process.cwd() + '/views/pug/index.pug', {title: 'Home Page', message: 'Please login', showLogin: true, showRegistration: true});
  .post((req, res, next) => {
      db.collection('users').findOne({ username: req.body.username }, function (err, user) {
          if(err) {
            console.log('Register Route: ' + err)
          } else if (user) {
            console.log('Register Route: User ' + user.ops[0].username + ' is already registered. Please log in')
          } else {
                {username: req.body.username,
                 password: req.body.password},
                (err, doc) => {
                    if(err) {
                      console.log('Register Route: Error ' + err)
                    } else {
                      console.log('Register Route: User ' + doc.ops[0].username + ' successfully saved to DB')
                        next(null, user);
    passport.authenticate('local', { failureRedirect: '/', successRedirect: '/profile' }),
    (req, res) => {
  console.log('Register Route > Passport Authenticate: User authenticated. Redirecting to profile page.')
      app.route('/login').post(passport.authenticate('local', { failureRedirect: '/' }), 
      (req, res) => {
        console.log('Login Route: User logged in, redirecting to profile.')
      res.render(res.render(process.cwd() + '/views/pug/profile.pug', {username: req.user.username, title: 'Profile Page'}))
    .get((req, res) => {
      console.log('Logout Route: User logged out')
    .get(passport.authenticate('local', {failureRedirect: '/'}), 
        (req, res) => {
       console.log('Profile Route: User was authenticated using ensureA function')
        res.render(process.cwd() + '/views/pug/profile.pug', {username: req.user.username, title: 'Profile Page'})
       app.use((req, res, next) => {
      .send('Not Found')
            app.listen(process.env.PORT, () => {
  console.log("Listening on port " + process.env.PORT);


How am I going to complete this if not even the direct code from github works

My glitch:

Please help.

Same here. I spent hours on this challenge and finally, the only solution I could find is:

  1. Use Firefox.
  2. timeout delays.
1 Like

It looks like you found the timeout code, uncomment that and like @TianW22 said use Firefox. That’s what got it to pass for me.

I also changed the title names in the pug file like other topics on the subject here suggested but that didn’t work. (The title changes were made before I used the timeout and switching to Firefox, but I don’t think that made a difference.) Also make sure in your .env file you have ENABLE_DELAYS=true;

Thanks! Yes, I tried the delays and it still didn’t pass. I did see some people had to play around with the timings on the delays. I’ll try that and see if it works. I’ve been working on this and really want to be done. Sooooo close.

shameless self promotion: Advanced Node and Express - Registration of New Users (Updated with only ONE hack)

I’m not really familiar with this exercise, but it looks like the signature for the get method is incorrect. On your glitch link you have

    .get(ensureAuthenticated, (req, res) => {...}

But the get method on route only accepts a function, or array of functions (according to the typescript typings). See below.

Try handling your logic in ensureAuthenticated rather than return next() from it. Hopefully that helps you out.

I may have missed it, but I don’t see you using the ensureAuthenticated function middleware.

I used it here

rather than passport.authenticate.

Also here

you use res.render(res.render(stuff you render)). Putting res.render inside res.render might be a problem. I haven’t tested whether it is or not, but either way it probably would only need to be used once.

I have finally got this to pass. I had to make the timeout longer but it is now working.