Session issues in production: /api/auth/session returns empty {} but works in development #11909
Unanswered
tayloraucoin
asked this question in
Help
Replies: 3 comments
-
Beta Was this translation helpful? Give feedback.
0 replies
-
@balazsorban44 why was this converted into a discussion? This seems to be a legitimate issue. |
Beta Was this translation helpful? Give feedback.
0 replies
-
any solutions so far? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Environment
Reproduction URL
https://github.com/tayloraucoin/next-auth-prisma-undefined-bug
Describe the issue
This is the step-by-step flow of the feature that is causing issues:
getSession
, which sends a verification request to next-auth./api/auth/session
is where the issue occurs in production—this endpoint returns{}
, while it works as expected in development.We are encountering session issues in production on Vercel with the Next.js app using next-auth. The issue arises during the
getSession
call, which fails to return a valid session. The session API request (/api/auth/session
) is returning{}
, and no session data is being retrieved for authenticated users, even though the cookies are properly set.In development, everything works as expected, and the
getSession
call successfully retrieves the session data. However, in production, the same logic results in an empty object being returned, and the user remains unauthenticated.We have used the
encode
anddecode
functions provided by next-auth to set anext-auth.session-token
cookie in the browser after validating a token passed in a URL query string. This setup works as expected in development but fails in production.I have tried adjusting cookie settings (e.g., SameSite, secure), but it does not resolve the issue.
Important: The callback functions with the nextauth setup are not being called in production. Only the
redirect
callback is. However, in development, I can see thejwt
andsession
callbacks are properly triggered. The absence of these callbacks is the biggest clue to what is going on. See./app/api/auth/[...nextauth]/route.ts
for reference.How to reproduce
Using the https://github.com/nextauthjs/next-auth-example template provided forces version 5. When trying to set next-auth to my actual version, several things broke. NextAuth from next-auth/next does not work in version 5. This has been frustrating, as the template is meant to save time. This example repo is also used for another issue with the Prisma adapter: #11773
In the setup, you can navigate to
http://localhost:3000/checkout-simulate
, enter an email, and click submit. This creates a user, generates an encoded JWT token, and produces a URL that redirects to/sign-in?token=${token}
.On the sign-in page, the token is read and sent to the
/api/auth/verify-token
endpoint for decoding and verification. This step works successfully, and the auth cookie is saved correctly. However, the/api/auth/session
request still returns an empty session{}
in production, even though everything works as expected in development.Expected behavior
In production, the /api/auth/session endpoint should return the proper session data as it does in development. The session callbacks should be triggered in production as they are in development.
Beta Was this translation helpful? Give feedback.
All reactions