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

next-auth v5: Bug when using the auth function inside getServerSideProps with nextjs v14 #12217

Open
trm217 opened this issue Nov 11, 2024 · 0 comments
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@trm217
Copy link

trm217 commented Nov 11, 2024

Environment

  System: irrelevant
  Binaries:
    Node: 21.2.0 - 
    Yarn: 1.22.22 - 
    npm: 10.2.3 - 
    pnpm: 9.12.3 - 
  Browsers: ittrlrbsnz
  npmPackages:
    next: 14.2.17 => 14.2.17
    next-auth: ^5.0.0-beta.25 => 5.0.0-beta.25
    react: ^18 => 18.3.1

Reproduction URL

https://github.com/trm217/next-auth-v5beta--nextjs-ssr-bug

Describe the issue

I found that next-auth can't be used inside getServerSideProps functions, when using next-auth v5 beta with Nextjs v14.
Using auth(ctx) inside getServerSideProps yields the following error:

CleanShot 2024-11-11 at 12 21 37

Code that causes this error:

export async function getServerSideProps(
  ctx: GetServerSidePropsContext
): Promise<GetServerSidePropsResult<Props>> {
  const session = auth(ctx);
  return {
    props: {
      message: !!session ? "Hi! :)" : "You're not logged in.",
    },
  };
}

How to reproduce

  1. Clone the reproduction & cd into the folder
  2. Install dependencies
  3. Create the env with npx auth secret
  4. Run the app in dev-mode using npm run dev
  5. Visit https://localhost:3000
  6. Login with the credentials username admin, password admin
  7. After successful authentication, you'll be redirected to http://localhost3000 where we attempt to load the session inside a getServerSideProps function. You'll see the error being thrown.

Expected behavior

We'd expect the auth function created through the NextAuth function to work as defined in the docs on how to use next-auth v5, where inside SSR within Next.js one should be able to call await auth(getServerSidePropsContext) to retrieve the session.

CleanShot 2024-11-11 at 12 19 44

https://authjs.dev/getting-started/migrating-to-v5 under authentication methods

@trm217 trm217 added bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

1 participant