Skip to content

Commit

Permalink
docs: added callout re getServerSession nextjs.md (#8964)
Browse files Browse the repository at this point in the history
added callout re getServerSession nextjs.md

Added an admonition/callout that specifies there is a contrast between the returning object using useSession (client components) and getServerSession (server components). Where the former appears to always return a session object, the latter only returns an object where there a authenticated cookies present, otherwise it returns null. I was expecting getServerSession to behave exactly like useSession, but this appears not to be the case and I would have appreciated if this was highlighted somewhere.

Co-authored-by: Nico Domino <[email protected]>
  • Loading branch information
Firgrep and ndom91 authored Dec 29, 2023
1 parent 9676afb commit c8edf2d
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/docs/configuration/nextjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ export default async function Page() {
}
```

:::info
In contrast to `useSession`, which will return a `session` object whether or not a user has logged in (whether or not cookies are present), `getServerSession` only returns a `session` object when a user has logged in (only when authenticated cookies are present), otherwise, it returns `null`.
:::

:::warning
Currently, the underlying Next.js `cookies()` method [only provides read access](https://beta.nextjs.org/docs/api-reference/cookies) to the request cookies. This means that the `expires` value is stripped away from `session` in Server Components. Furthermore, there is a hard expiry on sessions, after which the user will be required to sign in again. (The default expiry is 30 days).
:::
Expand Down

1 comment on commit c8edf2d

@vercel
Copy link

@vercel vercel bot commented on c8edf2d Dec 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.