Advanced Node and Express - Serialization of a User Object For Those Who are Stuck

I feel like this task wasn’t very clear and the whole callback is quite confusing. Took me a while to figure it out so to save you all some time I am posting the code below:

"use strict";

const express = require("express");
const fccTesting = require("./freeCodeCamp/fcctesting.js");
const pug = require('pug');
const session = require('express-session');
const passport = require('passport');
const ObjectID = require('mongodb').ObjectID;

const app = express();

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

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

passport.deserializeUser((id, done) => {
  db.collection('users').findOne(
    // {_id: new ObjectID(id)},
      (err, doc) => {
        done(null, null)
      }
  );
});

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.set('view engine', 'pug')

app.route("/").get((req, res) => {
  //Change the response to render the Pug template
  console.log('What is this', process.cwd())
  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);
});
1 Like

3 hours that I search in vain for a solution! Thanks @pinglinh :upside_down_face: