Created
June 19, 2014 19:28
-
-
Save hocza/472275433002ec723747 to your computer and use it in GitHub Desktop.
config/passport.js
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[0].id); | |
}); | |
passport.deserializeUser(function(id, done) { | |
User.findById(id, function (err, user) { | |
done(err, user[0]); | |
}); | |
}); | |
passport.use(new LocalStrategy( | |
function(username, password, done) { | |
User.findByUsername(username).done(function(err, user) { | |
if (err) { return done(null, err); } | |
if (!user || user.length < 1) { return done(null, false, { message: 'Incorrect User'}); } | |
bcrypt.compare(password, user[0].password, function(err, res) { | |
if (!res) return done(null, false, { message: 'Invalid Password'}); | |
return done(null, user); | |
}); | |
}); | |
}) | |
); | |
module.exports = { | |
express: { | |
customMiddleware: function(app){ | |
console.log('express midleware for passport'); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment