Created
December 22, 2015 08:16
-
-
Save zarzen/2f826171dce1561f2973 to your computer and use it in GitHub Desktop.
passport local strategy
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'); | |
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: '[email protected]' | |
}); | |
}); | |
})); | |
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