Advanced Node and Express - How to Use Passport Strategies

Tell us what’s happening:

The project is worked on local environment/computer
Tests don’t pass even though everything is written right or like shown in the check link in the description of the task/challenge page. [Solved]

Solution: Update the page on fCC(freeCodeCamp)

Your code so far


'use strict';
require('dotenv').config();

const express = require('express');

const myDB = require('./connection');

const fccTesting = require('./freeCodeCamp/fcctesting.js');

const session = require('express-session');

const passport = require('passport');

const { ObjectID } = require('mongodb');

const LocalStrategy = require('passport-local').Strategy;

const app = express();

app.set('view engine', 'pug');
app.set('views', './views/pug');

app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,
  cookie: { secure: false }
}));

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

myDB(async client => {
  const myDataBase = await client.db('database').collection('users');

  app.route('/').get((req, res) => {
    res.render('index', {
      title: 'Connected to Database',
      message: 'Please log in',
      showLogin: true
    });
  });

  app.route('/login').post(passport.authenticate('local', { failureRedirect: '/' }), (req, res) => {
    res.redirect('/profile');
  })

  app.route('/profile').get((req, res) => {
    res.render('profile');
  });
  
  passport.use(new LocalStrategy((username, password, done) => {
    myDataBase.findOne({ username: username }, (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);
    });
  }));

  passport.serializeUser((user, done) => {
    done(null, user._id);
  });
  
  passport.deserializeUser((id, done) => {
    myDataBase.findOne({ _id: new ObjectID(id) }, (err, doc) => {
      done(null, doc);
    });
  });

}).catch(e => {
  app.route('/').get((req, res) => {
    res.render('index', { title: e, message: 'Unable to connect to database' });
  });
});

app.use(passport.initialize());
app.use(passport.session());

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


Your browser information:

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

Challenge Information:

Advanced Node and Express - How to Use Passport Strategies

backend curriculum will be part of the new full stack certification, it will be updated

Try moving the two app.use up to where they are in the solution code. It is possible that because app is passed to fccTesting the middleware has to be mounted before that (didn’t test it).

app.use(passport.initialize());
app.use(passport.session());

fccTesting(app); // For fCC testing purposes

I’ve already solved it: the page needs to be reloaded. I mixed up words(update and reload) for some reason.
Though, users should know about that

I would assume if you use the dev script (using nodemon) the page would auto reload after the server restart. Does it not, or are you not using the dev script?

I didn’t check or modify original files, though, nodemon is in it(so, fcc page didn’t auto reload with it):

  "devDependencies": {
    "nodemon": "^2.0.4"
  }
}

I was talking about needing to reload the page here, btw: https://www.freecodecamp.org/learn/quality-assurance/quality-assurance-projects/metric-imperial-converter
nodemon only seems to reload the terminal whenever something is changed in the project