next-auth: Error: Invalid state returned from Google oAuth provider on production
Hello everyone,
My Next-Auth application works completely fine when am running this in local environment, the redirection from google, and session registration in MongoDb is working fine,
When am trying to run the same application with same google id & secret in production, it is throwing Error: Invalid state returned from oAuth provider
Already SET the valid NEXTAUTH_URL, DATABASE_URL, GOOGLE_ID & GOOGLE_SECRET
[next-auth][error][callback_oauth_error] Error: Invalid state returned from oAuth provider
at /home/node/app/node_modules/next-auth/dist/server/lib/oauth/callback.js:46:27
at Generator.next (<anonymous>)
at asyncGeneratorStep (/home/node/app/node_modules/next-auth/dist/server/lib/oauth/callback.js:26:103)
at _next (/home/node/app/node_modules/next-auth/dist/server/lib/oauth/callback.js:28:194)
at /home/node/app/node_modules/next-auth/dist/server/lib/oauth/callback.js:28:364
at new Promise (<anonymous>)
at /home/node/app/node_modules/next-auth/dist/server/lib/oauth/callback.js:28:97
at /home/node/app/node_modules/next-auth/dist/server/lib/oauth/callback.js:143:17
at /home/node/app/node_modules/next-auth/dist/server/routes/callback.js:58:31
at Generator.next (<anonymous>)
https://next-auth.js.org/errors#callback_oauth_error
Feedback Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.
- [] Found the documentation helpful
- Found documentation but was incomplete
- Could not find relevant documentation
- Found the example project helpful
- Did not find the example project helpful
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 7
- Comments: 31 (8 by maintainers)
I was able to reproduce this issue in my production app, and it ended up being the same problem mentioned by @Ash-Kay in the previous comment. Every time I accessed the web app without using “www” in the URL, the OAuth error would be thrown. In the same way, if I logged in using the WWW domain, and then manually removed the “www” portion of the URL and reloaded the page, the NextAuth session would be dropped. Like in @Ash-Kay’s case, I was able to make the changes to my Nginx config to redirect the non-WWW requests to the WWW domain.
For folks using next-pwa, latest version should fix this problem for you
Issue for me was my site was accessible with both www and non-www, my
NEXTAUTH_URLenv variable didn’t had www, so if someone access site with www there was issue in redirection, hence causing the error. Changed my NGINX config to redirect to non-www and it solved the issue.Hmm we have seen folks run into this before.
I think at least one of these can cause that problem.
We really need to create a ticket to capture improving the debugging here before I forget about it again - if we logged the response at this point it would probably be saying what was wrong in the console.
@returndiego
I’ve only just seen this message! Thanks for the detail. That’s really helpful.
Hey, that’s great! Disabling state for a provider using
state: falseis fine in NextAuth.js, because of how we check URLs before redirecting, so this should be safe to do. We provide the option by default because it works for most clients and is ideal behaviour for RFC compliance; there are only 1 or 2 clients that actually require it to work.I think how we are doing state has been refactored a bit in canary, so will be interesting to see if it resolves itself. If this fix resolves the issue on iOS though it’s perfectly fine and I think we’d consider making it the default for Google if we can’t get to the bottom of it.
I saw this as it was tagged in #1179 @timonweber and that’s interesting and might be worth exploring. I wonder if there is something else about how Safari works on iOS that is triggering this and that we might need to look at.
Welcome feedback from anyone else who has run into this issue and if the suggested fix resolves it for them.
I also had a problem with Google and Facebook providers not allowing me to log in from Safari on iOS. I’m not sure why, but I found out that it happens when I run the
signIn('provider_name')function. I solved this problem with a classic implementation using form tags to work around it. Although it is a bit tedious, this should ensure security.@iaincollins
Hey I’m having this same issue.
I am convinced that the latest version of iOS or Safari for iOS broke authentication with Google. I’ve tested this an ungodly amount of times. I ONLY get this error when I try to login from an iPhone. I tested Android, Windows and Chrome in MacOS. It’s all good. I’m absolutely positively sure that my code and callback URL’s, Client ID and Secret are correct. Otherwise, I would not be able to login from any other device.
Here’s my next-auth config.
@Ash-Kay @donovanperalta Solved it by redirecting my www to non www version of my domain. Amazing insight, thank you so much guys!!!
I’m having the same issue - can login fine on
localhost:3000but on my live link (hosted on vercel) I get redirected to my signup error page witherror?error=Callbackafter correctly entering my google credentials. Addingstate: falsedid not do anything to help unfortunatelyinteresting. could you enable logging with the
debug: trueoption and see if it tells anything useful?@iaincollins @balazsorban44 I apologize for the delay in testing. What I found out is that both
3.2.0-canary.35and3.1.0have different problems.3.2.0-canary.35
3.1.0
@fiftie I refactored
statein canary (#1172) , could you check out the latest release? (the state property is still there, but going forward protection: state will be the recommended way)@fiftie Thanks for that detail, will look at that in the upcoming release and see if I can reproduce and if we can come up with a fix. The client should definitely be doing that automatically.
@anerror404 Are you using PWA with your app? I think that’s what causing the issue. See here.
i am facing the same problem. It happens randomly, most of the time everything works just fine and sometime it’s not. I am using the same config.
If you’re using the
serverlessbuild target, try changing that toserver, it did the trick in my case.Im seeing the same issue - but only on first login of a session. Try immediately again and all is fine.
The error returns after logging in again after about
an hour24 hours, which i think is a new token.