Skip to content

Instantly share code, notes, and snippets.

@camperbot
Forked from ShaunSHamilton/server.js
Last active June 27, 2024 19:45
Show Gist options
  • Save camperbot/175f2f585a2d8034044c7e8857d5add7 to your computer and use it in GitHub Desktop.
Save camperbot/175f2f585a2d8034044c7e8857d5add7 to your computer and use it in GitHub Desktop.
Advanced Node and Express - Implement the Serialization of a Passport User
'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);
});
@Rosemary1995
Copy link

@niranad
Copy link

niranad commented Dec 28, 2021

I mean I have hours trying to figure out what I'm not doing right about this challenge!

@dwisatriow
Copy link

dwisatriow commented Jan 2, 2022

@niranad In my case the error is happen because I haven't set the environment variable on the platform where I hosted the app. After setting the environment variable the app is working fine

@niranad
Copy link

niranad commented Jan 2, 2022

@dwisatriow Thanks. I figured out it was a spelling error in my code. Appreciate that.

@igorgetmeabrain
Copy link

I cannot get this to work in replit. My pug template will no longer render, now that I've enclosed the GET within the code above (even though I've made sure the route to index.pug is correct). It just throws the error 'cannot GET /'.

@PCWCFA
Copy link

PCWCFA commented May 4, 2022

I cannot get this to work in replit. My pug template will no longer render, now that I've enclosed the GET within the code above (even though I've made sure the route to index.pug is correct). It just throws the error 'cannot GET /'.

Updated: the above render works. Just select the refresh button to trigger a get. BTW, do use the above res.render as my render below will render but won't pass the "there should be a database connection" test because apparently the database connection test is done on the content of the title field being exactly "Connected to Database", rather than, say, the myDataBase object. I have a serious concern with how the first test is written. It really should be updated to use the myDataBase object which contains namespace: MongoDBNamespace { db: 'database', collection: 'users' }, The test should not be written against the render.


My previous reply:

The render works if I update the render to what was used in the previous exercise. It also helps if you trigger a get by selecting the refresh button.

myDB(async client => {
const myDatabase = await client.db('database').collection('users');

app.get("/", (req, res) => {
res.render(process.cwd() + '/views/pug/index',
{title: 'Connected to DB', message: 'Please login'});
});

image

image

@igorgetmeabrain
Copy link

igorgetmeabrain commented May 4, 2022

Updated: the above render works. Just select the refresh button to trigger a get.

Thank you. Refreshing does work, as does opening the app in a separate browser tab. I spent sooo long reconfiguring my code and trying to figure out where I'd made a mistake and it turns out my code was fine all along!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment