PLEASE HELP! Implement serialization for a passport user not passing

Hello,

So this is my code (server.js) so far:

'use strict';
require('dotenv').config();
const express = require('express');
const myDB = require('./connection');
const fccTesting = require('./freeCodeCamp/fcctesting.js');
const passport = require('passport');
const session = require('express-session');
const ObjectID = require('mongodb').ObjectID;
const app = express();
app.set('view engine', 'pug');

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

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

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

  app.route('/').get((req, res) => {
    res.sender('pug', {
      title: 'Hello',
      message: 'Please login'
    })
    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('pug', { title: e, message: 'Unable to login' })
      })
  })
})


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

app.route('/').get((req, res) => {
  res.render(process.cwd() + '/views/pug/index', { title: 'Hello', message: "Please Login" });
});

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

Thanks for your help guys.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like