The user is undefined inside the NextAuth session callback
user is undefined in the session callbackShort 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.
Fix it fast
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 }, },2Match what you see, then jump to its fix
- 1
token is set and user is undefined in the session callbackJWT sessions pass token to the session callback, not userFix - 2
token.id is undefined, so session.user.id is undefined tooThe id was never copied into the token at sign-inFix - 3
the compiler rejects session.user.idTypeScript does not know user.id existsFix
- 1
What does the error look like?
user is undefined in the session callbacksession.user.id is undefinedTypeScript 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 user2Fix
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
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
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 --noEmit2Fix
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 --noEmitConfirmed by next-auth.js.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
NextAuth logs NO_SECRET, or throws it in production
In production, we expect you to define a secret property in your configuration
NextAuth needs a secret to encrypt its JWT and hash tokens.
2 causes, each with a fixWeb apps
TypeScript rejects the authorize function of the Credentials provider
A custom field you return from authorize is missing on the User or Session type
The authorize callback must return either a user object or null.
3 causes, each with a fixWeb apps
Upgrading to Next.js 16: middleware becomes proxy, and the other things that break
Missing Required default.js for Parallel Route
Next.js 16 renames middleware.ts to proxy.ts and its exported function to proxy.
6 causes, each with a fixWeb apps
next build stops because of TypeScript errors
How to disable typescript for failing my builds with nextjs?
next build runs a TypeScript check and fails the build when it finds type errors.
2 causes, each with a fixWeb apps
TypeScript cannot resolve import './globals.css' in Next.js
ts(2882)
TypeScript now checks imports like import './globals.css' when noUncheckedSideEffectImports is on, and it finds no type for a CSS file.
2 causes, each with a fixWeb apps
getStaticProps is ignored or rejected in a Next.js project
How to fetch data server-side in the latest Next.js? Tried getStaticProps but it's not running and getting undefined
getStaticProps belongs to the Pages Router.
4 causes, each with a fix