Skip to content

Instantly share code, notes, and snippets.

@prabod
Created February 9, 2017 05:32
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 prabod/0d20b644ad65fa92022b611b04dbaf2a to your computer and use it in GitHub Desktop.
Save prabod/0d20b644ad65fa92022b611b04dbaf2a to your computer and use it in GitHub Desktop.
app.get('/', function(req, res) {
res.render('index');
});
// PROFILE SECTION =========================
app.get('/profile', isLoggedIn, function(req, res) {
res.render('profile', {
user: req.user
});
});
// LOGOUT ==============================
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
// LOGIN ===============================
// show the login form
app.get('/login', function(req, res) {
res.render('login', {
message: req.flash('loginMessage')
});
});
// process the login form
app.post('/login', passport.authenticate('local-login', {
successRedirect: '/profile', // redirect to the secure profile section
failureRedirect: '/login', // redirect back to the signup page if there is an error
failureFlash: true // allow flash messages
}));
// SIGNUP =================================
// show the signup form
app.get('/signup', function(req, res) {
res.render('signup', {
message: req.flash('signupMessage')
});
});
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect: '/profile', // redirect to the secure profile section
failureRedirect: '/signup', // redirect back to the signup page if there is an error
failureFlash: true // allow flash messages
}));
// route middleware to ensure user is logged in
function isLoggedIn(req, res, next) {
if (req.isAuthenticated())
return next();
res.redirect('/');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment