节点JS Express JS App用户未被重定向
每当我尝试将用户重定向到仪表板时,服务器都会将GET请求接收到仪表板页面,但是用户页面不会重定向。它保留在登录页面上,用户收到cookie。
这是将会话密钥cookie和重定向发送到仪表板的登录函数。
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');
}
})
});
这是用户从登录帖子方法重定向时应该发生的仪表板调用。
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');
}
});
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');
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论