I have been attempting to redirect users to a login page after submitting a request to the home page if they are not logged in, but they keep getting redirected to the home page regardless. I have a method that is used to verify if they are logged in and returns a boolean with their status, then I have written another helper function using this method to redirect the user:
const verifySession = (req, res, next) => {
if (req.session) {
models.Sessions.get({hash: req.session.hash})
.then(sessionInfo => {
return models.Sessions.isLoggedIn(sessionInfo);
})
.then (status => {
if (!status) {
console.log(‘status’, status);
res.redirect('/login');
} else {
next();
}
});
}
};
The console.log for status is returning false as expected, but still no redirect is occurring. I have tried using this helper function in several different ways in my express request handler:
app.get('/',
(req, res) => {
verifySession(req, res);
res.render('index');
});
Or:
app.get('/', verifySession,
(req, res) => {
res.render('index');
});
Or also:
app.use('/', verifySession);
app.get('/',
(req, res) => {
res.render('index');
});
All of these have failed to redirect the user, and they are still being sent to the home page even when the status is returning false and the res.redirect call should be occurring.
Via Active questions tagged javascript - Stack Overflow https://ift.tt/Coqgp5D
Comments
Post a Comment