nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
25.01k stars 3.53k forks source link

SessionToken sometimes missing if the callback isn't specified #12139

Open ChloeCaronEng opened 3 weeks ago

ChloeCaronEng commented 3 weeks ago

What is the improvement or update you wish to see?

I was trying to fetch the session client side to get the sessionToken. The sessionToken would only sometimes be available in the session (e.g. when I refresh the page). To resolve this, I needed to add

 async session({ session }) {
      return session;
    },

in the session callbacks to force NextAuth to return the entire session each time. If this is the intended behaviour, it would be good to include this in the documentation.

Is there any context that might help us understand?

My setup:

  1. On the top level, I have everything wrapped in the following (note I need a custom basePath):

    <SessionProvider basePath="/auth" session={session}></SessionProvider>
  2. I am using useQuery to make a call to my custom NestJS backend and need to pass it the session token in the authorisation headers.

  3. When calling useSession, the sessionToken would only be present sometimes (I am assuming that there must be some caching)

  4. Adding the above forced it to always return the entire session object Note: I am using a Resend provider and a PrismaAdapter

I don't know if this is the intended behaviour but would be great to include it in the docs if so 🙏 Thank you!

Does the docs page already exist? Please link to it.

No response

srinesha commented 4 days ago

I'm facing the same issue. We have recently upgraded to next-auth@5.0.0-beta.25 with next@15.0.3 and after the authentication process has completed, await auth() in the session provider returns the session data successfully. In components with 'use client' successfully returns the session details when rendered in the server but on browser useSession returns null. However when the page is refreshed, useSession returns session data successfully.

Previously we were using 5.0.0-beta.16 with NextJs 14. Everything worked fine but I see this issue since we upgraded to 5.0.0-beta.25

srinesha commented 4 days ago

My issue is related to this https://github.com/nextauthjs/next-auth/issues/9504