nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
24.14k stars 3.35k forks source link

Docs: Bug in code example #11682

Open Jay-Karia opened 2 weeks ago

Jay-Karia commented 2 weeks ago

What is the improvement or update you wish to see?

The code examples listed here: https://authjs.dev/guides/extending-the-session#with-jwt as a Typescript error.

code_bug

Is there any context that might help us understand?

The code should be updated to:

 callbacks: {
    jwt({ token, user }) {
      if (user) {
        token.id = user.id;
      }
      return token;
    },
    session({ session, token }) {
      if (session.user) {
        session.user.id = token.id as string; // add "as string"
      }
      return session;
    },
  },

Does the docs page already exist? Please link to it.

https://authjs.dev/guides/extending-the-session#with-jwt

halvaradop commented 2 weeks ago

To remove these errors and enable autocompletion for the properties, you need to extend the JWT type in your project. For more information on how to do this, read more here. The default values for these types are:

interface JWT  {
  name?: string | null
  email?: string | null
  picture?: string | null
  sub?: string
  iat?: number
  exp?: number
  jti?: string
}

For this reason, the id property is unknown and shows as not assignable to type string.

Jay-Karia commented 2 weeks ago

IMO, this should be mentioned in the docs