Skip to content

Rolling session race condition #2335

@bartoszjuja

Description

@bartoszjuja

Checklist

Description

On my site with dozens of components I noticed that sometimes when a user tries to logout, the user is redirected through the logout process, but finally is still logged in. This seemed to happen mostly when trying to logout while parts of the site are still loading,

I analyzed this in the browser network tab and noticed that the reason are requests that are still in-flight while the user presses the logout button.
While the /auth/logout request correctly clears the session cookies, the in-flight requests sometimes get responses after /auth/logout and set the session back to active.

Timeline:
Image

A walkaround for me was to use the auth0/nextjs-auth0 middleware only on /auth paths, contrary to what is recommended in the documentation:

  if (path.startsWith("/auth/")) { // added this condition
    const authResponse = await auth0.middleware(request);
    // other auth code
  }
  // other site specific middleware

Obviously, this prevents rolling sessions from working.

Reproduction

Logging out while there are ongoing requests.

Additional context

No response

nextjs-auth0 version

4.9.0

Next.js version

15.5.2

Node.js version

20.19.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    ackbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions