Tell us what’s happening:
‘Implement the serialization of a passport user’ task.
‘Database connection’ test not passed.
Hi,
I’ve insert MONGO_URI in .env (mongodb+srv://lorenzoterreni1997:@cluster0.qwp9gtr.mongodb.net/) with correct password and no error is printed in console, and the pug view is correctly rendered. I’ve set ‘0.0.0.0/0’ in IP address list in mongoAtlas too.
In console it appear only a warning:
Could someone help me?
Your code so far
'use strict';
require('dotenv').config();
const express = require('express');
const session = require('express-session');
const passport = require('passport');
const { ObjectID } = require('mongodb');
const myDB = require('./connection');
const fccTesting = require('./freeCodeCamp/fcctesting.js');
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.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
cookie: { secure: false }
}));
app.use(passport.initialize());
app.use(passport.session());
app.set('view engine', 'pug');
app.set('views', './views/pug');
myDB(async client => {
const myDataBase = await client.db('chat_app').collection('users');
// Be sure to change the title
app.route('/').get((req, res) => {
// Change the response to render the Pug template
res.render('index', { title: 'Hello', 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('index', { title: e, message: 'Unable to connect to database' });
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log('Listening on port ' + PORT);
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Challenge Information:
Advanced Node and Express - Implement the Serialization of a Passport User