auth.js google login works on localhost, every vercel preview lands on /api/auth/error OAuth Callback
next-auth v5, Google provider. Works on localhost:3000 and works on the production domain.
On preview deployments it fails two different ways and I cannot work out why it is not consistent. Sometimes Google itself shows "Error 400: redirect_uri_mismatch" before I even pick an account. Sometimes I get the account picker, choose, and get bounced to /api/auth/error with nothing useful on it.
I have added about nine URLs to the authorised redirect list at this point and I am clearly doing this wrong.
@p99_hana · 3h ago · 3 replies
You are chasing two different bugs, which is why nothing you do fixes both.
Google's own error screen means the redirect URI in the request is not in your authorised list, exactly. Preview URLs change on every commit, so you will never win this by adding them - you would need a new entry per deploy.
Getting bounced to your own
/api/auth/errormeans Google was happy and your app rejected the callback. In v5 that is nearly always a missingAUTH_SECRETon the preview environment, or the host check - previews are not the URL you configured, so you needtrustHostset for the library to believe the incoming host.The real fix for the first one is a proxy: register exactly one stable redirect URI, something like
https://auth.yourdomain.com/api/auth/callback/google, and setredirectProxyUrlso previews send the OAuth round trip through that host and get handed back afterwards. One entry in the Google console, forever, regardless of how many branches are open.Reply
Report
@hemline_hana · 3h ago
Splitting the two symptoms is the useful part of this thread. Google's screen means the URI. Your error page means your app. Once you know which one you are looking at, both are twenty minutes.
Reply
Report
@thea_mandel · 3h ago
Also handle the
errorquery param on your error page and print it. The default page is deliberately vague for production and unhelpfully vague for you right now.Reply
Report