Created
July 16, 2015 05:51
-
-
Save Shekharrajak/4bbbbaff55d465575c4d to your computer and use it in GitHub Desktop.
For the tutorial on medium.com : passportJS auth
This file contains 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
var passport = require('passport'), | |
LocalStrategy = require('passport-local').Strategy, | |
bcrypt = require('bcrypt'); | |
passport.serializeUser(function(user, done) { | |
done(null, user.id); | |
}); | |
passport.deserializeUser(function(id, done) { | |
User.findOne({ id: id } , function (err, user) { | |
done(err, user); | |
}); | |
}); | |
passport.use(new LocalStrategy({ | |
usernameField: 'email', | |
passwordField: 'password' | |
}, | |
function(email, password, done) { | |
User.findOne({ email: email }, function (err, user) { | |
if (err) { return done(err); } | |
if (!user) { | |
return done(null, false, { message: 'Incorrect email.' }); | |
} | |
bcrypt.compare(password, user.password, function (err, res) { | |
if (!res) | |
return done(null, false, { | |
message: 'Invalid Password' | |
}); | |
var returnUser = { | |
email: user.email, | |
createdAt: user.createdAt, | |
id: user.id | |
}; | |
return done(null, returnUser, { | |
message: 'Logged In Successfully' | |
}); | |
}); | |
}); | |
} | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment