Skip to content

Type error in session callback for user and token properties #9633

@thomasmol

Description

@thomasmol

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriageUnseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions