Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

The user is undefined inside the NextAuth session callback

Erroruser is undefined in the session callback

Short answer

With JWT sessions, NextAuth gives the session callback a token, not a user. The user object only reaches the jwt callback once, at sign-in. Copy the id into the token there, then copy it from the token into session.user in the session callback. With database sessions, the session callback receives user instead.

  • Applies to next-auth 4, Next.js 15, TypeScript 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log the arguments the session callback really receives. If token is filled and user is empty, you are on JWT sessions and the fixes below apply.

    callbacks: {
      async session({ session, token, user }) {
        console.log({ hasToken: !!token, hasUser: !!user })
        return session
      },
    },
  2. 2Match what you see, then jump to its fix

    1. 1token is set and user is undefined in the session callbackJWT sessions pass token to the session callback, not userFix
    2. 2token.id is undefined, so session.user.id is undefined tooThe id was never copied into the token at sign-inFix
    3. 3the compiler rejects session.user.idTypeScript does not know user.id existsFix

What does the error look like?

  • user is undefined in the session callback
  • session.user.id is undefined
  • TypeScript error when reading session.user.id

Cause 1 of 3

JWT sessions pass token to the session callback, not user

1Check

With JWT sessions the session callback receives the token payload. The user object is only passed to it when you use database sessions.

// authOptions
session: { strategy: "jwt" } // JWT sessions: token, not user

2Fix

Read the value from token in the session callback and write it onto session.user. Do not read user there.

callbacks: {
  async session({ session, token }) {
    if (session.user) session.user.id = token.id
    return session
  },
}

3Verify

Call useSession() or getServerSession() and look at session.user.id.

console.log(session?.user?.id)

Confirmed by next-auth.js.org

Did this fix it?

Cause 2 of 3

The id was never copied into the token at sign-in

1Check

In the jwt callback, user, account and profile are only passed the first time it runs for a new session. On every later call only token is passed, so code that reads user outside the sign-in call finds nothing.

async jwt({ token, user }) {
  console.log("jwt call", { signingIn: !!user })
  return token
}

2Fix

Copy the value into the token inside an if (user) check. Later calls keep it, because the token carries it.

callbacks: {
  async jwt({ token, user }) {
    if (user) token.id = user.id
    return token
  },
  async session({ session, token }) {
    if (session.user) session.user.id = token.id as string
    return session
  },
}

3Verify

Sign in again and confirm session.user.id has a value on a protected page.

console.log(session?.user?.id)

Confirmed by next-auth.js.org

Did this fix it?

Cause 3 of 3

TypeScript does not know user.id exists

1Check

The default Session type has no id field, so the compiler rejects session.user.id even when it is set at runtime.

npx tsc --noEmit

2Fix

Add a declaration file that extends the Session interface, and keep the default user fields with DefaultSession. Make sure the folder holding it is covered by typeRoots in tsconfig.json.

// types/next-auth.d.ts
import NextAuth, { DefaultSession } from "next-auth"

declare module "next-auth" {
  interface Session {
    user: {
      id: string
    } & DefaultSession["user"]
  }
}

3Verify

Run the type check again.

npx tsc --noEmit

Confirmed by next-auth.js.org

Did this fix it?

How do I stop it coming back?

  • Write down which session strategy the project uses; the callback arguments depend on it.
  • Put every value the session needs into the token in one place, the jwt callback.
  • Keep one next-auth.d.ts file so the Session type matches what the callbacks return.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.