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

Jonathon Brandt :

In Node.js, I used the passportJS LocalStrategy to authenticate the users. 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.

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. 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? Any suggestions?

FOR MORE INFO: Here is what my passport serialization/deserialization and passport.use functions look like:

Again, the getAuthenticated function works perfectly. 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.

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));
    });
}
Anatoly :

You should wait a promise in deserializeUser:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I get current session's username using Passport.js?

Why am I unable to access req.user.username while using passport.js?

How do I get the current Windows logged-in user's username?

How do I get the username of the current user in a Flask app on an IIS server using Windows Authentication?

How can I get the current user's username in Bash?

How I get all users except the current_user by username

I want to understand how passport js work specially req.user and how data is populated

how to get soundcloud username with passport.js

How do I access Passport's req.user variable in client side javascript?

How do I get the current username in .NET using C#?

How do I get the current username in Windows PowerShell?

passport's req.isAuthenticated always returning false, even when I hardcode done(null, true)

How to get current username or User account in Python

How do I get a reference to the current Promise within then()

Passport.js & Express Session - req.user undefined causing .isAuthenticted() to return false

Passport.js, Sessions, & React | Accessing req.user from any route other than the /login route is undefined

How do I get user's current voice channel using discord.js?

From AWS SDK, how to I get the current logged in username (or IAM user)?

I am trying to get the current session username to be inserted into a table PHP SESSIONS

How do I get the current user Id without hard coding it in?

How do I get environment variables for the current user in Windows (SHELL)?

How do I get the current user details for a phpFox website?

How do I troubleshoot CheckLib returning false?

Express Passport.js: req.user VERSUS req.session.passport.user

How can I get the user's username?

How can I get user by userName in GraphQL?

How do i return true/false on get-content command for each username inside C:/Users?

how to get user data with req.user on decorator in nest js

How is req.isAuthenticated() in Passport JS implemented?