#Please Help: NextAuth Deployment to Vercel Resulting in api/auth/error

156 messages · Page 1 of 1 (latest)

azure raptor
#

Hello, I am trying to deploy my NextJs web app to Vercel, but for some reason anytime I try to login I end up getting to the page WEBSITE_URL/api/auth/error. It's extremely confusing because when I build it locally and run, everything works as expected. I am 100% certain that I have provided the right NEXT_AUTHURL to the environment variables but it still seems to not work 😦
It's been extremely frustrating because I can't seem to figure out why I keep getting the error, I've attached a screenshot of what the log is.

Environment variables:
NEXTAUTH_URL = https://NAME_OF_APP_HERE.vercel.app
NEXTAUTH_SECRET = SECRET_HERE

azure raptor
#

:/

odd cosmos
#

can you reproduce this in a minimal app?

#

it sounds like something I have faced before (some dejavu kicking in here) but I can't recall exactly what the bug was

#

(also, make sure all packages are at latest version)

azure raptor
#

Did it involve the env variables ?

odd cosmos
#

your env variables look fine

azure raptor
#

I can make a minimal app, but it will take some time if it’s okay

odd cosmos
#

no problems

azure raptor
#

I’ll start by posting the code that I’m using in my …nextauth file for reference however

#

It works completely fine locally

#

But it’s only during deployment is when it fails

#

Particularly with vercel

odd cosmos
#

an easy way to make a repro would be

  1. npm create next-app@latest
  2. set up next-auth according to the guide on their website
  3. copy your next-auth related config over in small batches
#

then eventually the bug will show up, give us the repo link at that point

azure raptor
#

Well here's the thing

#

It's more than likely going to work locally though when I run and build it

odd cosmos
#

understood, just make a repo

azure raptor
#

will do

odd cosmos
#

btw on vercel you dont need NEXTAUTH_URL though

#

try removing that (although I doubt it will help much)

#

remove the URL var on vercel only

#

keep it in local .env.local

azure raptor
#

Okay give me a moment

#

Thank you

azure raptor
#

All done

odd cosmos
#

on local it works fine

#

so not your problem but an equally interesting problem 🤔

azure raptor
#

That I've deployed on vercel

#

It uses the same nextauth file

odd cosmos
azure raptor
#

Same

#

And it doesn't redirect me to the hello page

#

Smh

#

The env variables I've included

odd cosmos
#

lemme see whats wrong

azure raptor
#

This is the other app

#

If you try to click on any, I get an api/auth/error

#

But again, it only works locally

odd cosmos
#

btw you can go to the functions tab to see the live log

#

which very likely has some error messages printed there

#

they are very helpful

azure raptor
#

None actually!

odd cosmos
#

wait really?

azure raptor
#

Yes, this is what's been so confusing for the past hours

#

This is all I see

odd cosmos
#

wait a bit

azure raptor
#

and it just stays like this even when I refresh or click new pages

#

But it remains like this

odd cosmos
#

then go to your app and click on the button

#

then it will display something there

#

either 200 success or xxx error

#

if it still doesnt display something the only reason i can think of is your adblocker

azure raptor
#

Okay

#

It looks like logs are coming in now

#

However, this isn't really helpful

#
[next-auth][error][CLIENT_FETCH_ERROR] 
https://next-auth.js.org/errors#client_fetch_error Unexpected token '<', "<!DOCTYPE "... is not valid JSON Object
error @ _app-37e2a24dd2f1b18c.js:1
#

I see this error in the console though

#

But Stackoverflow said this is harmless

#

When i read docs, when there's a [CLIENT_FETCH_ERROR], it means to add the ENV but it's already included

odd cosmos
#

bug for your repo found

azure raptor
#

???

odd cosmos
#

probably not the bug about this client fetch error though

#

const isSecure = process.env.NODE_ENV === "production";

#

is correct

#

the other isSecure is not since the env var is not present in there

#

in function getSessionFromCookie inside [...nextauth].ts

azure raptor
#

Does this line not work? const isSecure = process.env.NEXT_PUBLIC_BASE_URL?.startsWith("https://")

#

Where it checks if its a https

odd cosmos
azure raptor
#

Ah

#

Hmm

odd cosmos
#

if you are referring to the vercel variable it's NEXT_PUBLIC_VERCEL_URL instead

azure raptor
#

Wait

odd cosmos
#

but tbf just check for NODE_ENV, works most of the time already

#

and simple

azure raptor
#

Do I need to add that manually?

odd cosmos
#

just check for NODE_ENV

azure raptor
#

Okay

#

Let me try that

odd cosmos
#

along with some other handy ones

azure raptor
#

Did it work for you when you tried it?

odd cosmos
odd cosmos
azure raptor
#

TRYING NOW ON THE REAL VERSION

odd cosmos
#

moral of the story: don't use too many env vars and always check you have provided the values for all used env vars before deploying

#

these are the variables deployed on the working version btw

#

if you need it

#

among them NEXTAUTH_SITE and NEXT_PUBLIC_NEXTAUTH_URL are unnecessary as well, probably there because you added them in desperation

azure raptor
#

I'm still getting the error

odd cosmos
#

well you need another reproduction repo then...

#

and above all, upgrade next-auth because i recall a bug relating to this issue in a previous version

#

latest next-auth version is 4.18.2

azure raptor
#

I have "next-auth": "^4.18.7",

odd cosmos
#

did you come from the future

#

ah oops i came from the past instead

#

latest version is 4.19.2

#

upgrade it to "next-auth": "^4.19.2"

#

and redeploy

#

without all the unnecessary env variables since they can unexpectedly mess with the built-in ones

azure raptor
#

Most def

#

Question

#

I'm using yarn

#

yarn upgrade next-auth@^4.19.2

odd cosmos
#

i use https://npmjs.com/npm-check-updates so i don't really remember those commands anymore sorry

azure raptor
#

redploying now

#

Sigh

#

This is ridiculous

#

Still not working :/

azure raptor
#

as the env variables

#

But it doesn't make any sense how I'm still getting an api/auth/error

#

And it seems the literal only error that I'm getting is [next-auth][error][CLIENT_FETCH_ERROR]

odd cosmos
azure raptor
#

third times the charm (hopefully 🙏🏾 ) deploying

odd cosmos
#

(and after each env change, please redeploy)

#

if it doesn't work, i have no choice but to ask you for a different reproduction repo

azure raptor
#

I have to produce another branch 😭 I'll try to have this within the next 30 mins if you would still be able to help me, truly appreciated it

#

Unless you'd be okay being a temp contributor?

odd cosmos
#

it's 00:04 here though so perhaps tmr is more likely

azure raptor
#

Understandable

#

Sigh

#

Any thoughts on what you think it could be after seeing my code?

odd cosmos
azure raptor
#

You're good

#

It's public

#

This is the repository

odd cosmos
#

try renaming /api (the root api dir) to something else

#

reason: if /api exists, it's likely vercel will take it for the serverless functions instead

#

and the pages/api won't work like you expect it to

azure raptor
#

Nice

#

Redeploying

#

IT"S WORKING!!!

#

THANK YOU SOOOO MUCH @odd cosmos

odd cosmos
#

nice

#

glad it worked

#

now time to clean up the mess

odd cosmos
#

revoke all those secrets immediately

#

and generate new ones

azure raptor
#

Yeah definiely rotating them

#

For sure, thank you

azure raptor
#

@odd cosmos I just wanted to personally thank you again for your help today, it truly means a lot