Skip to content

Instantly share code, notes, and snippets.

@miyamotodev123
Last active January 8, 2016 03:26
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 miyamotodev123/4a112567e92c4981e253 to your computer and use it in GitHub Desktop.
Save miyamotodev123/4a112567e92c4981e253 to your computer and use it in GitHub Desktop.
// app/routes.js
module.exports = function (app, passport) {
// LOGOUT ==============================
app.post('/logout', function(req, res) {
req.logout();
res.json({ redirect: '/logout' });
});
// =====================================
// SIGNUP ==============================
// =====================================
// process the signup form
app.post('/signup', function(req, res, next) {
// if email or password is missing, return an error message
if (!req.body.email || !req.body.password) {
return res.status(400).json({ error: 'Email and Password required' });
}
passport.authenticate('local-signup', function(err, user, info) {
if (err) {
return res.status(400).json(err);
}
if (user.error) {
return res.status(400).json({ error: user.error });
}
req.logIn(user, function(err) {
if (err) {
return res.status(400).json(err);
}
return res.status(200).json({ redirect: '/profile' });
});
})(req, res);
});
// =====================================
// LOGIN ==============================
// =====================================
// process the login form
app.post('/login', function (req, res, next) {
if (!req.body.email || !req.body.password) {
return res.status(400).json({error: 'Email and Password required.'});
}
passport.authenticate('local-login', function (err, user, info) {
if (err) {
return res.status(400).json({error: err});
}
if (user.error) {
return res.status(400).json({error: user.error});
}
req.logIn(user, function (err) {
if (err) {
return res.status(400).json(err);
}
return res.status(200).json({redirect: '/profile'});
});
})(req, res);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment