-
-
Save camperbot/2d06ac5c7d850d8cf073d2c2c794cc92 to your computer and use it in GitHub Desktop.
Advanced Node and Express - Clean Up Your Project with Modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const passport = require('passport'); | |
const LocalStrategy = require('passport-local'); | |
const bcrypt = require('bcrypt'); | |
const ObjectID = require('mongodb').ObjectID; | |
module.exports = function (app, myDataBase) { | |
passport.serializeUser((user, done) => { | |
done(null, user._id); | |
}); | |
passport.deserializeUser((id, done) => { | |
myDataBase.findOne({ _id: new ObjectID(id) }, (err, doc) => { | |
if (err) return console.error(err); | |
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); | |
}); | |
} | |
)); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const passport = require('passport'); | |
const bcrypt = require('bcrypt'); | |
module.exports = function (app, myDataBase) { | |
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, showRegistration: true }); | |
}); | |
app.route('/login').post(passport.authenticate('local', { failureRedirect: '/' }), (req, res) => { | |
res.redirect('/profile'); | |
}); | |
app.route('/profile').get(ensureAuthenticated, (req, res) => { | |
res.render('pug/profile', { username: req.user.username }); | |
}); | |
app.route('/logout').get((req, res) => { | |
req.logout(); | |
res.redirect('/'); | |
}); | |
app.route('/register').post( | |
(req, res, next) => { | |
const hash = bcrypt.hashSync(req.body.password, 12); | |
myDataBase.findOne({ username: req.body.username }, function (err, user) { | |
if (err) { | |
next(err); | |
} else if (user) { | |
res.redirect('/'); | |
} else { | |
myDataBase.insertOne({ username: req.body.username, password: hash }, (err, doc) => { | |
if (err) { | |
res.redirect('/'); | |
} else { | |
next(null, doc.ops[0]); | |
} | |
}); | |
} | |
}); | |
}, | |
passport.authenticate('local', { failureRedirect: '/' }), | |
(req, res, next) => { | |
res.redirect('/profile'); | |
} | |
); | |
app.use((req, res, next) => { | |
res.status(404).type('text').send('Not Found'); | |
}); | |
}; | |
function ensureAuthenticated(req, res, next) { | |
if (req.isAuthenticated()) { | |
return next(); | |
} | |
res.redirect('/'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 routes = require('./routes'); | |
const auth = require('./auth.js'); | |
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'); | |
routes(app, myDataBase); | |
auth(app, myDataBase); | |
}).catch((e) => { | |
app.route('/').get((req, res) => { | |
res.render('pug', { title: e, message: 'Unable to login' }); | |
}); | |
}); | |
app.listen(process.env.PORT || 3000, () => { | |
console.log('Listening on port ' + process.env.PORT); | |
}); |
Shouldn't Line 22 in auth.js be:
if (!bcrypt.compareSync(password, user.password)) { return done(null, false); }
I think @LucaswithC is right; we've already implemented bcrypt at this point.
Shouldn't Line 22 in auth.js be:
if (!bcrypt.compareSync(password, user.password)) { return done(null, false); }
Yes I think He is right.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 40 of server.js should be:
const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log('Listening on port ' + PORT); });
Right now it passes the spec but the console says: Listening on port undefined