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

feat(next): allow unstable_getServerSession in Server Components #5741

Merged
merged 8 commits into from Nov 6, 2022

Conversation

balazsorban44
Copy link
Member

Next.js 13 shipped with the new app/ directory that implements React Server Components. With the recently introduced change of allowing async/await directly, there is a need for accessing the session in server components in a clean way.

With this PR, unstable_getServerSession will now work in Server Components as well:

// app/page.tsx
import { unstable_getServerSession } from "next-auth/next"
import { authOptions } from "pages/api/auth/[...nextauth]"

export default async function Page() {
  const session = await unstable_getServerSession(authOptions)
  return <pre>{JSON.stringify(session, null, 2)}</pre>
}

Notes:

  1. Currently, the underlying Next.js cookies() method does only provides read access 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).

  2. Since app/ is experimental in Next.js, we are considering this to be experimental in NextAuth.js so we can iterate on the implementation if upstream changes require it.

Fixes #5647

@vercel
Copy link

vercel bot commented Nov 6, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Updated
next-auth ⬜️ Ignored (Inspect) Nov 6, 2022 at 4:02AM (UTC)

@github-actions github-actions bot added the core Refers to `@auth/core` label Nov 6, 2022
Copy link
Member

@ThangHuuVu ThangHuuVu left a comment

Choose a reason for hiding this comment

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

🚢

@glenarama
Copy link

glenarama commented Nov 7, 2022

While this appears to work in dev - i get no session when hosted on Vercel. Is anyone else able to confirm if this is true or perhaps related to my setup?
Fixed as i was writing this :-)

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Refers to `@auth/core`
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Next.js 13 app directory
3 participants