Code refactoring of redirect to last URL
This commit is contained in:
@ -9,15 +9,7 @@ var User = require('../models/User');
|
||||
|
||||
exports.getLogin = function(req, res) {
|
||||
if (req.user) return res.redirect('/');
|
||||
|
||||
// If the host in the header of the User Agent making the request is
|
||||
// the same as the server host, keep a copy of the referrer url and
|
||||
// redirect to that later
|
||||
if(req.header('host') == 'localhost:3000'){
|
||||
req.session.referrer = req.header('Referrer');
|
||||
console.log(req.session.referrer);
|
||||
}
|
||||
|
||||
req.session.lastUrl = req.header('Referer');
|
||||
res.render('account/login', {
|
||||
title: 'Login'
|
||||
});
|
||||
@ -43,18 +35,14 @@ exports.postLogin = function(req, res, next) {
|
||||
|
||||
passport.authenticate('local', function(err, user, info) {
|
||||
if (err) return next(err);
|
||||
|
||||
if (!user) {
|
||||
req.flash('errors', { msg: info.message });
|
||||
return res.redirect('/login');
|
||||
}
|
||||
|
||||
req.logIn(user, function(err) {
|
||||
if (err) return next(err);
|
||||
req.flash('success', { msg: 'Success! You are logged in.' });
|
||||
// redirecting toa referrer page else to home page
|
||||
if(req.session.referrer) return res.redirect(req.session.referrer);
|
||||
return res.redirect('/')
|
||||
res.redirect(req.session.lastUrl || '/');
|
||||
});
|
||||
})(req, res, next);
|
||||
};
|
||||
|
Reference in New Issue
Block a user