Skip to content

req.isAuthenticated() returning false immediately after login #482

@jamesplease

Description

@jamesplease

StackOverflow question here.

I've been fighting for quite awhile with this bug: immediately after the user authenticates with, say, Google, req.isAuthenticated() is returning false. I'm persisting sessions to a Postgres DB, which seems to be working fine. It's just the call to isAuthenticated which leads me to wonder if my Passport configuration might be wrong, or something.

My code for the callback looks like:

const redirects = {
  successRedirect: '/success',
  failureRedirect: '/failure'
};
app.get('/auth/google/callback', passport.authenticate('google', redirects));

My very last middleware logs the value of req.isAuthenticated(). It logs false when Google redirects back to my page, but if the user manually refreshes then it returns true.

Here are detailed logs of the logging in process:

# this is the first request to the login page. Not logged in yet.
4:59:54 PM web.1 |  -----New request-----
4:59:54 PM web.1 |  route: /login authenticated: false

# they've clicked the link to `/login`, and are immediately forwarded to Google
4:59:59 PM web.1 |  -----New request-----

# they've granted across through Google; Google redirects back to app.
# Using the Google profile, we get the user's account from the user account table
5:00:06 PM web.1 |  -----New request-----
5:00:06 PM web.1 |  about to fetch from DB
5:00:07 PM web.1 |  retrieved user from DB

# redirection to the success page...`authenticated` is false? It should now be true!
5:00:07 PM web.1 |  -----New request-----
5:00:07 PM web.1 |  route: /success authenticated: false

# here's a manual refresh...now they're showing as authenticated
5:05:34 PM web.1 |  successful deserialize
5:05:34 PM web.1 |  -----New request-----
5:05:34 PM web.1 |  route: /success authenticated: true

It looks like deserialize isn't being called when Google redirects back to my app; could that be the source of the issue?

Source code:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions