Last active
January 8, 2016 03:26
-
-
Save miyamotodev123/4a112567e92c4981e253 to your computer and use it in GitHub Desktop.
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
// 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