Skip to content

Commit

Permalink
Update extending-the-session.mdx
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsorban44 committed Sep 26, 2024
1 parent 78200dc commit 942f20f
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions docs/pages/guides/extending-the-session.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ This is `name`, `email`, and `image`.

A common use case is to add the user's id to the session. Below it is shown how to do this based on the session strategy you are using.

<Callout type="info">
By default, the `id` property does not exist on `token` or `session`. See the [TypeScript](https://authjs.dev/getting-started/typescript) on how to add it.
</Callout>

## With JWT

To have access to the user id, add the following to your Auth.js configuration:

```ts filename="auth.ts"
providers,
// By default, the `id` property does not exist on `token` or `session`. See the [TypeScript](https://authjs.dev/getting-started/typescript) on how to add it.
callbacks: {
jwt({ token, user }) {
if (user) { // User is available during sign-in
Expand All @@ -39,16 +43,12 @@ Then, to expose the user's id in the actual session, you can access `token.id` i

Calls to `auth()` or `useSession()` will now have access to the user's id.

<Callout type="warning">
By default `id` property does not exists in `jwt`, see the [Typescript Docs](https://authjs.dev/getting-started/typescript) for more info.
</Callout>

## With Database

If you are using a database session strategy, you can add the user's id to the session by modifying the `session` callback:

```ts filename="auth.ts"
providers,
// By default, the `id` property does not exist on `session`. See the [TypeScript](https://authjs.dev/getting-started/typescript) on how to add it.
callbacks: {
session({ session, user }) {
session.user.id = user.id
Expand Down

0 comments on commit 942f20f

Please sign in to comment.