Advanced Node and Express -Issues on passport Strategies

Good Day Guys,
I am trying to pass this Topic on strategies in Advanced Node and Express but my code not working
here is my code:

'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 ObjectID = require('mongodb').ObjectID;
const mongo = require('mongodb').MongoClient();
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.urlencoded({ extended: true }));
app.set('view engine', 'pug')
  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUnintialized: true,



mongo.connect(process.env.MONGO_URI, (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) => {

      {_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(user, null);

   .get((req, res) => {

    res.render(process.cwd() + '/views/pug/index', {title: 'Hello',message: 'Please login', showLogin: true});
    .post(passport.authenticate('local', {failureRedirect: '/'}),
     function(req, res) {
    .get((req, res) => {
      res.render(process.cwd() + 'views/bug/profile');

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

what did I do wrong??

Haven’t run your code but…

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

You have not terminated your else block above, you need to close it , right now all your searilzation / desearilzation, passport config and routes are inside the mongoose.connect callback, they shouldn’t be…

I have passed the test thanks pro I just have to change the title of index.pug page to my Home Page…I have done so at first but then there was an error message keep telling me that searilzation and desearilzation, passport and routes config should be inside mongo.connect callback I think it should be in the else code cause if it connects to database then authentication and user’s serialization and deserialization has to be checked

Ah, just realized you were using .MongoClient() and not mongoose, good you passed it anyhow

1 Like