Skip to content

Instantly share code, notes, and snippets.

@cultofmetatron
Created April 9, 2013 21:38
Show Gist options
  • Save cultofmetatron/5349630 to your computer and use it in GitHub Desktop.
Save cultofmetatron/5349630 to your computer and use it in GitHub Desktop.
passport ajax capable authenticate
app.post('/login', function(req, res) {
console.log(res);
passport.authenticate('local', function(err, user) {
if (req.xhr) {
//thanks @jkevinburton
if (err) { return res.json({ error: err.message }); }
if (!user) { return res.json({error : "Invalid Login"}); }
req.login(user, {}, function(err) {
if (err) { return res.json({error:err}); }
return res.json(
{ user: {
id: req.user.id,
email: req.user.email,
joined: req.user.joined
},
success: true
});
});
} else {
if (err) { return res.redirect('/login'); }
if (!user) { return res.redirect('/login'); }
req.login(user, {}, function(err) {
if (err) { return res.redirect('/login'); }
return res.redirect('/');
});
}
})(req, res);
});
@beshur
Copy link

beshur commented Aug 5, 2014

Great stuff, thanks.
Check out my fork for outputting custom error messages generated by Strategy: https://gist.github.com/beshur/a84a60369036d0ba560a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment