Skip to content

Instantly share code, notes, and snippets.

@camperbot
Forked from ShaunSHamilton/server.js
Last active December 5, 2022 05:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save camperbot/7ad011ac54612ad53188b500c5e99cb9 to your computer and use it in GitHub Desktop.
Save camperbot/7ad011ac54612ad53188b500c5e99cb9 to your computer and use it in GitHub Desktop.
Advanced Node and Express - How to Use Passport Strategies
'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 LocalStrategy = require('passport-local');
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',
showLogin: true
});
});
app.route('/login').post(passport.authenticate('local', { failureRedirect: '/' }), (req, res) => {
res.redirect('/profile');
});
app.route('/profile').get((req, res) => {
res.render(process.cwd() + '/views/pug/profile');
});
// 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);
});
});
passport.use(new LocalStrategy(
function(username, password, done) {
myDataBase.findOne({ username: username }, function (err, user) {
console.log('User '+ username +' attempted to log in.');
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (password !== user.password) { return done(null, false); }
return done(null, user);
});
}
));
// 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);
});
@Schawnnara
Copy link

Schawnnara commented Nov 12, 2021

thank you so much freecodecamp

@remchaf
Copy link

remchaf commented Jun 18, 2022

thank you freecodecamp ,

@qwertydiy2
Copy link

thank you!!!!!!!!!!!!!!!!!!!!!!!!!

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