'Node JS express JS app User not getting redirected
Whenever I try and redirect the user to the dashboard, the server receives the get request to the dashboard page but the user's page does not get redirected. It remains on the login page, the cookie is received by the user.
This is the login function that sends the session key cookie and the redirection to the dashboard.
app.post('/login', function(req, res){
var email = req.body.email;
var password = req.body.password;
User.findOne({'email' : email}, function(err, user){
if (err)
{
console.log(err);
}
if (user)
{
var hash = user.password;
if (passwordManagement.verifyPassword(password, hash))
{
var newSession = webSession.generateKey(email)
webSession.SessionKeys.push(newSession);
res.cookie('session', newSession.key);
return res.redirect(303, '/dashboard');
}
else
{
res.cookie('session', 'logErr');
return res.redirect(303, '/login');
}
}
else
{
res.cookie('session', 'logErr');
return res.redirect(303, '/login');
}
})
});
This is the dashboard call that should occur when the user is redirected from the login POST method.
app.get('/dashboard', function(req, res){
var sessionKey = req.cookies.session
if (sessionKey != undefined)
{
var email = webSession.findEmailFromSessionKey(sessionKey);
if (email)
{
User.findOne({'email' : email}, function(err, user){
if (err)
{
console.log(err);
}
if (user)
{
fs.readFile(process.cwd() + '/Web Pages/Public/Logged/Dashboard.html', 'utf8', function(err, data){
if (err) {console.log('FS READ Dashboard Error:\n' + err)}
else
{
data = webSession.generateDashboard(user, data);
fs.writeFile('temp.html', data, function(err){
if (err) {console.log('FS WRITE Dashboard Error:\n' + err)}
else
{
console.log('Sending DASHBOARD file');
res.sendFile(process.cwd() + '/temp.html', function(err){
if (err) {console.log('RES Dashboard Error:\n' + err)}
else
{
fs.unlink(process.cwd() + '/temp.html', function(err){
if (err) {console.log('FS UNLINK Dashboard Error:\n' + err)};
});
}
});
}
})
}
});
}
else
{
res.cookie('session', '');
return res.redirect('/login');
}
})
}
else
{
res.cookie('session', '');
return res.redirect('/login');
}
}
else
{
res.cookie('session', '');
return res.redirect('/login');
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
