next-auth: NEXTAUTH_URL has been set but it still shows [next-auth][error][CLIENT_FETCH_ERROR]
Description 🐜
The Next auth is working fine on local and on vercel it throws [next-auth][error][CLIENT_FETCH_ERROR]
Although I have set JWT and NEXTAUTH_URL in my vercel env creds
https://google-places-unravel.vercel.app/
Is this a bug in your own project?
Yes
How to reproduce ☕️
import NextAuth from "next-auth";
import GithubProvider from "next-auth/providers/github";
import GoogleProvider from "next-auth/providers/google";
import { getToken } from "next-auth/jwt"
export default NextAuth({
// Configure one or more authentication providers
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
// ...add more providers here
],
jwt: {
// A secret to use for key generation. Defaults to the top-level `secret`.
secret: process.env.JWT_SECRET,
// The maximum age of the NextAuth.js issued JWT in seconds.
// Defaults to `session.maxAge`.
maxAge: 60 * 60 * 24 * 30,
// You can define your own encode/decode functions for signing and encryption
// if you want to override the default behavior.
async encode({ secret, token, maxAge }) {},
async decode({ secret, token }) {},
}
});
Screenshots / Logs 📽
Environment 🖥
Vercel.com
Contributing 🙌🏽
Yes, I am willing to help solve this bug in a PR
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 15
- Comments: 27 (1 by maintainers)
For anyone else still having this issue with none of the above solutions working I changed the secret variable to NEXT_PUBLIC_SECRET and put it above the providers in […nextauth]. I also removed secret from the jwt callback
Hello, I’m facing the same issue.
.env file
dependencies
_app.tsx
logs
I do have the same error in production but things work fine in development!
Okay solution for this is to add secret: process.env.SECRET above jwt and it’ll work on PROD
@JW709
Just FYI, anything prefixed with
NEXT_PUBLIC_
will be surfaced as a public environment variable in your app (i.e., will be accessible from the client side).Clear cache and cookie of your browser and the error will be removed! I fixed this exact error just by clearing cache and cookie for the last 24 hours!
Hope this fixes the error (worked for me), https://stackoverflow.com/questions/71600978/adding-secret-still-gives-error-in-nextauth/71606643#71606643
Also facing the same issue 😦 reproduction at https://instagram-navy-nu.vercel.app/
NextAuth code at https://github.com/richardHaggioGwati/Instagram/blob/master/pages/api/auth/%5B…nextauth%5D.ts
Same thing with me but slightly different. I had set secret property inside and outside jwt property in NextAuth options object and that was a problem. I left only outside secret property and it fixed the problem.
@vashisth00 What do you mean by this? Can you share what your NextAuth config object looks like? Is
process.env.SECRET
an environment variable you set yourself?I opened a PR, but I’ll have to dig deep as it’s actually the final fix. It is more likely that we just want to remove the
jwt.secret
config option in favor of the top-levelsecret
one, since it’s already not being used:https://github.com/nextauthjs/next-auth/blob/c71cb8457da0febe5d71283e9361014dda652c31/src/core/lib/utils.ts#L37
So as a workaround for your use case, you can just move the
secret
out of thejwt
object, and the issue should go away.