Tell us what’s happening:
Test fails, and I cant solve why
'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').ObjectID;
const app = express();
app.set('view engine', 'pug')
const port = process.env.PORT || 8080;
//for fCC TESTING
fccTesting(app); //For FCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
//use express-session
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
cookie: { secure: false }
}));
app.use(passport.initialize());
app.use(passport.session());
// // RENDER CONTENT
// app.route('/').get((req, res) => {
// res.render(process.cwd() + '/views/pug', {title: 'Hello', message: 'Please login'});
// });
myDB(async (client) => {
// my db and coll
const myDataBase = await client.db('database').collection('users');
app.route('/').get((req, res) => {
// Change the response to render the Pug template
res.render('pug', {
title: 'Connected to Database',
message: 'Please login'
});
});
// SErialization and DEserialization
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' });
});
});
//LISTEN ON PORT 3000
app.listen(port, () => {
console.log('Listening on port ' + process.env.PORT);
});
solution: https://chatapp-doki050.herokuapp.com
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36.
Challenge: Implement the Serialization of a Passport User
Link to the challenge:
