Tell us what’s happening:
Successfully connected to DB but cannot pass 2nd test: Deserialization should now be correctly using the DB and done(null, null)
should be erased.
Your code so far
"use strict";
require('dotenv').config();
const express = require("express");
const fccTesting = require("./freeCodeCamp/fcctesting.js");
const session = require("express-session");
const passport = require("passport");
const mongo = require('mongodb').MongoClient;
const app = express();
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
//res.send(`Pug template is not defined.`);
//res.render('pug/index');
res.render(process.cwd() + '/views/pug/index', {title: 'Hello', message: 'Please login'});
});
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
}));
app.use(passport.initialize());
app.use(passport.session());
const ObjectID = require('mongodb').ObjectID;
mongo.connect(process.env.DATABASE, {useUnifiedTopology: true, useNewUrlParser: true}, (err, db) => {
if(err) {
console.log('Database error: ' + err);
} else {
console.log('Successful database connection');
//serialization and app.listen
passport.serializeUser((user, done) => {
done(null, user._id);
});
passport.deserializeUser((id, done) => {
//done(null, null);
db.collection('users').findOne(
{_id: new ObjectID(id)},
(err, doc) => {
done(null, doc);
}
);
});
app.listen(process.env.PORT || 3000, () => {
console.log("Listening on port " + process.env.PORT);
});
}
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36
.
Challenge: Implement the Serialization of a Passport User
Challenge Link: https://www.freecodecamp.org/learn/quality-assurance/advanced-node-and-express/implement-the-serialization-of-a-passport-user