Next Auth getSession API call fails randomly on production
Asked Answered
V

1

6

I have set my NEXTAUTH_URL in Vercel. My NextJS app works fine most of the time, but randomly when I navigate to a new page, I'll get a 500 server error when the next-auth useSession() hook is called. When I inspect the network calls in Chrome I can see this endpoint failing with a 500 server error: /api/auth/session

The console log says:

[next-auth][error][client_fetch_error] 
https://next-auth.js.org/errors#client_fetch_error session 
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

If I refresh the page, the endpoint will return a 200 OK and be able to return the session data via useSession()

This is a pretty bad experience for my users, because as they navigate around the website, fetching their session will randomly fail. I'm not sure how to solve this error, because it happens so sporadically.

I've made sure to set my NEXTAUTH_URL environment variable on Vercel. And fetching the user session does work some of the time. But the failures are happening more often than I'd like.

Vankirk answered 1/8, 2021 at 2:36 Comment(1)
Did you solve this? I am facing a similar issue with nexauth right now. I have also set the NEXTAUTH_URL env variable but it keeps failing in production. Locally everything works and I'm getting the same error like you but with a 404.Brittenybrittingham
S
1

I faced the same issue with the latest version on next-auth 4.0.6 and even 4.0.1 but got it working with v4.0.0-beta.4 by setting the NEXTAUTH_URL env parameter and CLIENT_ID and SECRET. But would need to investigate what has changed in the latest version.

Storehouse answered 4/1, 2022 at 6:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.