简体   繁体   English

passport.js req.user 正在返回:'Promise { false }' - 如何获取当前会话的用户名?

[英]passport.js req.user is returning: 'Promise { false }' - How do I get the current sessions' username?

In Node.js, I used the passportJS LocalStrategy to authenticate the users.在 Node.js 中,我使用了passportJS LocalStrategy 来验证用户。 And this function还有这个功能

req.getAuthenticated()

Allows me to get whether the current session is authenticated.允许我获取当前会话是否经过身份验证。 I also wanted the server to return the username of the current session.我还希望服务器返回当前会话的用户名。 To do that, I tried accessing the user property of req.为此,我尝试访问 req 的用户属性。

if(req.isAuthenticated()){
    let user = req.user.username;
}  

But it is returning 'undefined'.但它返回“未定义”。 I continued with...我继续...

if(req.isAuthenticated()){
    console.log(req.user);
}

And it says that the req.user is a 'Promise{ false }', for some reason.它说 req.user 是一个 'Promise{ false }',出于某种原因。 I, and then tried out我,然后尝试了

if(req.isAuthenticated()){
    let user = await req.user;
    console.log(user);
}

And it returns 'false'.它返回“假”。 This is the first time I've worked with the passport.js so I may have set it up wrong because req.user should not return a promise, right?这是我第一次使用passport.js,所以我可能设置错了,因为req.user 不应该返回一个promise,对吧? Any suggestions?有什么建议?

FOR MORE INFO: Here is what my passport serialization/deserialization and passport.use functions look like:更多信息:这是我的护照序列化/反序列化和passport.use函数的样子:

Again, the getAuthenticated function works perfectly.同样,getAuthenticated 函数可以完美运行。 When users are logged on, they are authenticated, and when they're not, they're not authenticated.当用户登录时,他们通过身份验证,而当他们没有登录时,他们不进行身份验证。 I just can't get the req.user properties.我只是无法获得 req.user 属性。

const initialize = (passport, UserCollection, getUserFromUsername, getUserFromId) => {
    const authenticateUser = async (username, password, done) => {
        let user = await getUserFromUsername(UserCollection, username);
        if(user === null){
            return done(null, false, {message: 'The user with that username does not exist in the database.'});
        }
        try{
            if(await bcrypt.compare(password, user.password)){
                return done(null, user);
            }
            else{
                return done(null, false, {message: 'Passwords do not match'});
            }
        }
        catch {
            return done(null, false, {message: 'There was a problem logging you in.'})
        }
    }

    passport.use(new LocalStrategy(authenticateUser));
    passport.serializeUser((user, done) => {
        return done(null, user._id);
    });
    passport.deserializeUser((id, done) => {
        return done(null, getUserFromId(id));
    });
}

You should wait a promise in deserializeUser :您应该在deserializeUser等待承诺:

passport.deserializeUser(async (id, done) => {
        const user = await getUserFromId(id);
        return done(null, user);
    });

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

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