Tell us what’s happening:
I have trouble trying to pass, Implement the Serialization of a Passport User.
I need help to pass the Database connection should be present.
I am able to pass the second test,
Deserialization should now be correctly using the DB and
done(null, null)
should be called with thedoc
.
But for some reason, I cannot pass the first test,
Database connection should be present.
- What is wrong in my replit code that does not allow the database connection?
- Is there an error in the
server.js file
or in thesample.env file
? - Where should I fix in my replit code? Is it in the
package.json file
,sample.env file
,server.js file
or theconnection.js file
? - I added my
MONGO_URI
code into thesample.env file
. I rather not show it containing my password…
PORT=8080
NODE_ENV=development
SESSION_SECRET="tryagaincoding"
MONGO_URI="my mongodb code"
Your code so far
'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');
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());
myDB(async (client) => {
const myDataBase = await client.db('database').collection('users');
// Be sure to change the title
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 here...
passport.serializeUser((user, done) => {
done(null, user._id);
});
passport.deserializeUser((id, done) => {
myDataBase.findOne({ _id: new ObjectID(id) }, (err, doc) => {
done(null, doc);
});
});
// Be sure to add this...
}).catch((e) => {
app.route('/').get((req, res) => {
res.render('pug', { title: e, message: 'Unable to login' });
});
});
// app.listen out here...
app.listen(process.env.PORT || 3000, () => {
console.log('Listening on port ' + process.env.PORT);
});
My link to my Replit:
https://replit.com/@ArtC/boilerplate-advancednode-2#server.js
I appreciate the help.
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36
Challenge: Implement the Serialization of a Passport User
Link to the challenge: