next-auth: Type error in session callback for `user` and `token` properties
Environment
npmPackages: @auth/core: ^0.* => 0.20.0 @auth/drizzle-adapter: ^0.* => 0.3.14 @auth/sveltekit: ^0.* => 0.5.2 Node: 20.3.1 - /opt/homebrew/bin/node
Reproduction URL
https://github.com/thomasmol/authjsbugs
Describe the issue
related to #9437
i get a type error in the session callback for the user or token property:
Property 'user' does not exist on type '({ session: Session; user: AdapterUser; } | { session: Session; token: JWT; }) & { newSession: any; trigger?: "update" | undefined; }'.
error claims user does not exist, while it does, although it depends on the database strategy used.
error located here on the user property in hooks.server.ts:
callbacks: {
session: async ({ session, user }) => {
if(session.user){
session.user.id = user.id;
}
return session;
}
},
not sure if useful but this is copilot explaining: "The error message you’re seeing is due to TypeScript not being able to determine the exact type of the object that’s being passed to the session callback. The type of the object is a union type, which means it could be one of several types. In this case, it could be { session: Session; user: AdapterUser; } or { session: Session; token: JWT; }.
The user property exists on the first type, but not on the second. When you try to access user.id, TypeScript can’t guarantee that user will always be present, hence the error."
How to reproduce
install auth core + auth sveltekit, create hooks.server.ts and add a the session callback. type error occurs on user and token, but not the session property.
Expected behavior
no type errors
About this issue
- Original URL
- State: closed
- Created 6 months ago
- Reactions: 12
- Comments: 16 (1 by maintainers)
Commits related to this issue
- fix(types): For Property 'user' or 'token' does not exist on types (#9633) — committed to ajstars1/next-auth by ajstars1 5 months ago
- Fixed user session type mismatch in the latest Next Auth update https://github.com/nextauthjs/next-auth/issues/9633#issuecomment-1899645783 — committed to FlorianLeChat/Simple-File-Storage by FlorianLeChat 5 months ago
Can confirm this on the latest
next-auth: 5.0.0-beta.5FYI for a quick fix, just manually set the types while setting user as optional, e.g.
Downgrading to beta.4 was the easiest and least hacky fix that worked for me. Make sure to delete the ^ symbol.
Before:
After:
async session({ session, token}: {session: Session, token?: any}) {this is what worked for me you can also try as far i understood this is a typescript error .I was able to pass the error putting ‘?’ on the token. I’m in Next Auth Beta 5.
This seems to be related to https://github.com/Microsoft/TypeScript/issues/14094.
Minimum code:
https://www.typescriptlang.org/play?#code/C4TwDgpgBA8gRgKwIxQLxQN5QIYC4oDOwATgJYB2A5gDRRz5FlVQC+AUKJLIgExqY4GJCjSgBjIU0qs2bMQHtyRKADMAruTH8AFNmKUC+eMigAfbgh4BKNAD5MbKE5z6CAOjj8ARF7bsgA
Using
{ session, user?, token? }instead of{ session, user } | { session, token }seems to fix this.i think #9646 is the solution to this as proposed by @nbifrye. It is more due to a limitation in typescript rather than authjs