节点JS Express JS App用户未被重定向

发布于 2025-01-25 04:02:40 字数 3357 浏览 3 评论 0原文

每当我尝试将用户重定向到仪表板时,服务器都会将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文