nextauthjs / nextauthjs/next-auth

SessionToken sometimes missing if the callback isn't specified

Open
#12,139 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
TypeScript
Stars
28.4k
Forks
4k
PR merge metrics
No merged PRs in 30d

Description

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>
  1. 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.
  2. When calling useSession, the sessionToken would only be present sometimes (I am assuming that there must be some caching)
  3. 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the SessionProvider configured with basePath="/auth" and the useSession call, then compare behavior with and without the session callback shown in the report. Verify whether sessionToken is intentionally omitted or intermittently unavailable, and document the confirmed callback or configuration behavior if that is the intended result.

Written by the indexing model from the issue text.

Assessment

Tech stack
nextjs, react, typescript
Domain
authentication, documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.