Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
thomasmol opened this issue Jan 13, 2024 · 16 comments · Fixed by #9756
Closed

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

thomasmol opened this issue Jan 13, 2024 · 16 comments · Fixed by #9756
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@thomasmol
Copy link

thomasmol commented Jan 13, 2024

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

@thomasmol thomasmol added bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Jan 13, 2024
@nbifrye
Copy link
Contributor

nbifrye commented Jan 15, 2024

This seems to be related to microsoft/TypeScript#14094.

Minimum code:

type Obj1 = { a: string, b: string }
type Obj2 = { a: string, c: string }

const func = (args: Obj1 | Obj2) => {
    args.b = ""
         ^
         Property 'b' does not exist on type 'Obj1 | Obj2'.
           Property 'b' does not exist on type 'Obj2'.
}

https://www.typescriptlang.org/play?#code/C4TwDgpgBA8gRgKwIxQLxQN5QIYC4oDOwATgJYB2A5gDRRz5FlVQC+AUKJLIgExqY4GJCjSgBjIU0qs2bMQHtyRKADMAruTH8AFNmKUC+eMigAfbgh4BKNAD5MbKE5z6CAOjj8ARF7bsgA

Using { session, user?, token? } instead of { session, user } | { session, token } seems to fix this.

@Kinbaum
Copy link

Kinbaum commented Jan 18, 2024

Can confirm this on the latest next-auth: 5.0.0-beta.5

Screenshot 2024-01-18 at 9 17 07 AM

@BernardinoOtais
Copy link

Can confirm this on the latest next-auth: 5.0.0-beta.5

Screenshot 2024-01-18 at 9 17 07 AM

It's also happening to me...

@sahilq312
Copy link

Can confirm this on the latest next-auth: 5.0.0-beta.5

Screenshot 2024-01-18 at 9 17 07 AM

i am also facing the same problem . did yu find any solution

@thomasmol
Copy link
Author

i think #9646 is the solution to this as proposed by @nbifrye. It is more due to a limitation in typescript rather than authjs

@igmtink
Copy link

igmtink commented Jan 19, 2024

I have same problem

@jeremy-code
Copy link

FYI for a quick fix, just manually set the types while setting user as optional, e.g.

import NextAuth, { type Session, type User } from "next-auth";

export const auth = NextAuth({
  callbacks: {
    async session({ session, user }: { session: Session; user?: User }) {
      if (user) {
        // do whatever here
      }
      return session;
    },
  },
});

@BernardinoOtais
Copy link

FYI for a quick fix, just manually set the types while setting user as optional, e.g.

import NextAuth, { type Session, type User } from "next-auth";

export const auth = NextAuth({
  callbacks: {
    async session({ session, user }: { session: Session; user?: User }) {
      if (user) {
        // do whatever here
      }
      return session;
    },
  },
});

Taking in consideration what jeremy-code posted, this is how I did it:

import NextAuth, { Session, User } from "next-auth";

async session(
  params:
    | { session: Session; user: User }
    | { session: Session; token: JWT },
) {
  if (!("token" in params)) return params.session;

  const sessao = {
    ...params.session,
    token: params.token,
  };
  return sessao;
},

}

@ypanagidis
Copy link

Same here, when trying to access the token in the session callback using the jwt strategy, it wont let me access the second part of the params which included the token. Only present in authjs 5 beta 5, not in authjs 5 beta 4.

@sahilq312
Copy link

Same here, when trying to access the token in the session callback using the jwt strategy, it wont let me access the second part of the params which included the token. Only present in authjs 5 beta 5, not in authjs 5 beta 4.

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 .

@ayinde-xyz
Copy link

I am also having the same error

@Ealanisln
Copy link

On my case I fix the first error (Property token does not exist on type session Session user) but now the token is not being recognized.

Screenshot 2024-01-21 at 4 24 47 PM

@hellogbg
Copy link

My experience is that it works on 5.0.0-beta.4
but not on 5.0.0-beta.5

@Ealanisln
Copy link

I was able to pass the error putting '?' on the token. I'm in Next Auth Beta 5.

Screenshot 2024-01-22 at 12 30 08 PM

@travistylervii
Copy link

Downgrading to beta.4 was the easiest and least hacky fix that worked for me. Make sure to delete the ^ symbol.

Before:
CleanShot 2024-01-23 at 22 26 04@2x

After:
CleanShot 2024-01-23 at 22 26 32@2x

@SamuelOliveira-M
Copy link

SamuelOliveira-M commented May 1, 2024

In my case I solved it as follows :
image

This way, there is no longer an error in the token parameter of the session object and I can even add more fields if I want within the token in addition to those that already come by default.
https://authjs.dev/getting-started/typescript

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet