next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

Docs: Bug in code example

Open Jay-Karia opened this issue 1 year ago • 2 comments

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

Jay-Karia avatar Aug 27 '24 03:08 Jay-Karia

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.

halvaradop avatar Aug 27 '24 18:08 halvaradop

IMO, this should be mentioned in the docs

Jay-Karia avatar Aug 28 '24 08:08 Jay-Karia

This issue was marked with the good first issue label by a maintainer.

This means that it is a good candidate for someone interested in contributing to the project, but does not know where to start.

Have a look at the Contributing Guide first.

This will help you set up your development environment to get started. When you are ready, open a PR, and link back to this issue in the form of adding Fixes #1234 to the PR description, where 1234 is the issue number. This will auto-close the issue when the PR gets merged, making it easier for us to keep track of what has been fixed.

Please make sure that - if applicable - you add tests for the changes you make.

If you have any questions, feel free to ask in the comments below or the PR. Generally, you don't need to @mention anyone directly, as we will get notified anyway and will respond as soon as we can)

[!NOTE]
There is no need to ask for permission "can I work on this?" Please, go ahead if there is no linked PR :slightly_smiling_face:

github-actions[bot] avatar Sep 25 '24 22:09 github-actions[bot]

Simply linking to https://authjs.dev/getting-started/typescript should be sufficient

balazsorban44 avatar Sep 25 '24 22:09 balazsorban44

Currently, there is a link that navigates to the TypeScript docs, or your idea would be to add a link in the base description ?

halvaradop avatar Sep 25 '24 22:09 halvaradop