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.
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