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

fix(ts): improve unstable_getServerSession return type #5792

Merged
merged 5 commits into from
Nov 24, 2022

Conversation

balazsorban44
Copy link
Member

@balazsorban44 balazsorban44 commented Nov 11, 2022

Currently, unstable_getServerSession assumes that it always returns a Session. However, in practice, it should match the session callback's return type. This can be used to create feature parity with getToken like this:

// app/page.tsx
import { getToken } from "next-auth/jwt"
import { unstable_getServerSession } from "next-auth/next"
import { headers, cookies } from "next/headers"

export default async function Page() {
  const session = await unstable_getServerSession({
    callbacks: { session: ({ token }) => token },
  }) // session will take the shape of JWT

  const token = await getToken({
    req: {
      headers: Object.fromEntries(headers()),
      cookies: Object.fromEntries(cookies().getAll().map((c) => [c.name, c.value])),
    } as any,
  })

  // These two should look equal
  return (
    <>
      <pre>{JSON.stringify(session, null, 2)}</pre>
      <pre>{JSON.stringify(token, null, 2)}</pre>
    </>
  )
}

This is an alternative to #5791, reducing the number of APIs needed in the future (no need for getToken). (See #5791 (comment))

Closes #5791, fixes #5754

@vercel
Copy link

vercel bot commented Nov 11, 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 24, 2022 at 1:42PM (UTC)

@github-actions github-actions bot added the core Refers to `@auth/core` label Nov 11, 2022
@balazsorban44 balazsorban44 temporarily deployed to Preview November 11, 2022 03:20 Inactive
@github-actions
Copy link

github-actions bot commented Nov 11, 2022

🎉 Experimental release published 📦️ on npm!

pnpm add next-auth@0.0.0-pr.5792.9119ea67
yarn add next-auth@0.0.0-pr.5792.9119ea67
npm i next-auth@0.0.0-pr.5792.9119ea67

@balazsorban44 balazsorban44 marked this pull request as ready for review November 11, 2022 04:30
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.

I really like this approach, save us from exposing another API! 👍 Added 2 comments

packages/next-auth/src/next/index.ts Show resolved Hide resolved
packages/next-auth/src/next/index.ts Outdated Show resolved Hide resolved
@balazsorban44 balazsorban44 temporarily deployed to Preview November 24, 2022 13:43 Inactive
@balazsorban44 balazsorban44 merged commit a307079 into main Nov 24, 2022
@balazsorban44 balazsorban44 deleted the fix/infer-gss-return-type branch November 24, 2022 14:13
@@ -143,7 +153,7 @@ export async function unstable_getServerSession(
} else {
req = args[0]
res = args[1]
options = args[2]
options = Object.assign(args[2], { providers: [] })
Copy link
Contributor

@ammmze ammmze Dec 6, 2022

Choose a reason for hiding this comment

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

Is it intentional that we are modifying args[2] here? I recently updated next-auth and started getting some odd behavior where I've got places in my app using my authOptions object and it's fine at first, but eventually the providers got cleared out...this is the reason for that.

I suspect we meant to do options = Object.assign({}, args[2], { providers: [] }) instead? Or IMO use the spread operator options = { ...args[2], providers: [] }

Copy link
Contributor

Choose a reason for hiding this comment

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

I've opened a PR that should fix this.

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.

Allow getToken in Server Components
3 participants