Skip to content

Instantly share code, notes, and snippets.

@zarzen
Created December 22, 2015 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zarzen/2f826171dce1561f2973 to your computer and use it in GitHub Desktop.
Save zarzen/2f826171dce1561f2973 to your computer and use it in GitHub Desktop.
passport local strategy
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
module.exports = (function () {}(
return {
configureApp : function (app){
passport.use(new LocalStrategy(function (username, password, done) {
process.nextTick(function() {
done(null, {
userName: 'admin',
isAdmin: true,
email: 'admin@domain.com'
});
});
}));
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (obj, done) {
done(null, obj);
});
app.use(passport.initialize());
app.use(passport.session());
app.post('/login', function (req, res, next){
passport.authenticate('local', function (err, user, info) {
if(!user){
res.json({
status: false,
err: info.message
});
}else{
req.logIn(user, function (err){
if (err) {return next(err);}
res.json({
status: true
});
});
}
})(req, res, next);
});
app.get('/logout', function (req, res){
req.logOut();
res.redirect('/login');
});
}
};
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment