简体   繁体   English

用户使用护照注册后登录

[英]Logging in users after they register using passport

After a user signs up, I'd like them to automatically be logged in. I read online to use req.login(), but it doesn't seem to be working.用户注册后,我希望他们自动登录。我在线阅读以使用 req.login(),但它似乎不起作用。 The user is added to the database, but aren't logged in. Can someone point me the right direction?用户被添加到数据库中,但没有登录。有人能指出我正确的方向吗?

        if(type == 'student'){
            User.saveStudent(newUser, newStudent, function(err, user){
                console.log('Student saved');
                req.login(newUser, function(err) {
                    if (err) {
                        console.log(err);
                    }
                })
            })
        } else {
            User.saveInstructor(newUser, newInstructor, function(err, user){
                console.log('Instructor saved');
                req.login(newUser, function(err) {
                    if (err) {
                        console.log(err);
                    }
                });
            })
        }

        res.redirect('/classes');
    }
});

module.exports = router;

OH, I see what's happening.哦,我明白发生了什么。 You're doing a res.redirect() outside the callback where you save the user.您正在保存用户的回调之外执行res.redirect() It's probably redirecting prior to the login cookie being set.它可能在设置登录 cookie 之前重定向。

Move the res.redirect() into the res.login() callback and you should be good.res.redirect()移动到 res.login() 回调中,您应该会很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM