javascript - res.redirect 在 Node.js Express 和 Passport.js 身份验证后不起作用

标签 javascript node.js express passport.js

我在使用 res.redirect() 进行页面重定向时遇到问题。

这是我的代码片段。

console.log('user=', req.user); - 这是有效的。在此之后,页面正在加载但仍在加载中。发生超时错误。

res.redirect('/chat/loginsuccess'); - 这不起作用。

有没有人遇到过这个问题?请帮助我。

...
router.post('/login', passport.authenticate('chat-login'), function(req, res, next) {
    console.log('user=', req.user);
    return res.redirect('/chat/loginsuccess');
});

...
passport.use('chat-login', new LocalStrategy({
    // by default, local strategy uses username and password, we will override with email
    usernameField : 'email',
    passwordField : 'password'
}, function(email, password, done) {
    if (email) {
        // Use lower-case e-mails to avoid case-sensitive e-mail matching
        email = email.toLowerCase();
    }

    // asynchronous
    process.nextTick(function() {
        ChatUser.findOne({ 'local.email' :  email }, function(err, user) {
            // if there are any errors, return the error
            if (err) {
                return done(err);
            }

            // if no user is found, return the message
            if (!user) {
                return done(null, false, { message: 'No User Found.' });
            }

            if (!user.validPassword(password)) {
                return done(null, false, { message: 'Oops! Wrong Password.' });
            }

            // success
            return done(null, user);
        });
    });

}));
...

最佳答案

您可以简单地使用 passport 的 successRedirect 和 failureRedirect 选项。

您的登录路径将类似于:

router.post('/login', passport.authenticate('chat-login', {
                                             successRedirect : '/whereEverYouWantIfsuccessfullyLoggedIn',
                                             failureRedirect : '/whereEverYouWantIffailedToLogIn'
}));

关于javascript - res.redirect 在 Node.js Express 和 Passport.js 身份验证后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50038539/

相关文章:

javascript - Googlemap 标记 - 图像淡入淡出

javascript - 将流传输到无处

node.js - 处理 MongoDB 从 Node 断开/重新连接

node.js - 启动 nodejs : openssl config failed 时出错

javascript - 如何在 UIAutomation 中使用 Node 模块

javascript - 为什么 Sequelize 只使用 findAll() 返回一个对象?

javascript - Mongoose find 使用expressjs查找if语句错误

javascript - 如何在react(firebase)中为runTransaction创建新的文档引用?

javascript - 使用express js 返回(发送)int

javascript - 简单的 Highcharts 图表在 Firefox 中不起作用