Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
PORT = 2121
DB_STRING = mongodb+srv://demo:demo@cluster0.hcds1.mongodb.net/todos?retryWrites=true&w=majority
DB_STRING = mongodb+srv://chrisnelson7791:wegoget@cluster0.0v9lcln.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
28 changes: 24 additions & 4 deletions config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,32 @@ const LocalStrategy = require('passport-local').Strategy
const mongoose = require('mongoose')
const User = require('../models/User')

// module.exports = function (passport) {
// passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
// User.findOne({ email: email.toLowerCase() }, (err, user) => {
// if (err) { return done(err) }
// if (!user) {
// return done(null, false, { msg: `Email ${email} not found.` })
// }
// if (!user.password) {
// return done(null, false, { msg: 'Your account was registered using a sign-in provider. To enable password login, sign in using a provider, and then set a password under your user profile.' })
// }
// user.comparePassword(password, (err, isMatch) => {
// if (err) { return done(err) }
// if (isMatch) {
// return done(null, user)
// }
// return done(null, false, { msg: 'Invalid email or password.' })
// })
// })
// }))

module.exports = function (passport) {
passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
User.findOne({ email: email.toLowerCase() }, (err, user) => {
passport.use(new LocalStrategy({ usernameField: 'username' }, (username, password, done) => {
User.findOne({ userName: username }, (err, user) => {
if (err) { return done(err) }
if (!user) {
return done(null, false, { msg: `Email ${email} not found.` })
return done(null, false, { msg: `username ${username} not found.` })
}
if (!user.password) {
return done(null, false, { msg: 'Your account was registered using a sign-in provider. To enable password login, sign in using a provider, and then set a password under your user profile.' })
Expand All @@ -17,7 +37,7 @@ module.exports = function (passport) {
if (isMatch) {
return done(null, user)
}
return done(null, false, { msg: 'Invalid email or password.' })
return done(null, false, { msg: 'Invalid username or password.' })
})
})
}))
Expand Down
5 changes: 3 additions & 2 deletions controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const User = require('../models/User')

exports.getLogin = (req, res) => {
if (req.user) {
console.log('** this user is already logged in - req.user: ', req.user)
return res.redirect('/todos')
}
res.render('login', {
Expand All @@ -13,14 +14,14 @@ const User = require('../models/User')

exports.postLogin = (req, res, next) => {
const validationErrors = []
if (!validator.isEmail(req.body.email)) validationErrors.push({ msg: 'Please enter a valid email address.' })
// if (!validator.isEmail(req.body.email)) validationErrors.push({ msg: 'Please enter a valid email address.' })
if (validator.isEmpty(req.body.password)) validationErrors.push({ msg: 'Password cannot be blank.' })

if (validationErrors.length) {
req.flash('errors', validationErrors)
return res.redirect('/login')
}
req.body.email = validator.normalizeEmail(req.body.email, { gmail_remove_dots: false })
// req.body.email = validator.normalizeEmail(req.body.email, { gmail_remove_dots: false })

passport.authenticate('local', (err, user, info) => {
if (err) { return next(err) }
Expand Down
Loading