getToken method returns null in vercel preview deployment #4387
Replies: 6 comments 8 replies
-
We had a similar issue where the tokens are stored as secured cookies, but getToken was assuming they are not secure.
Adding the secureCookie option for getToken worked for our Vercel preview deploys. |
Beta Was this translation helpful? Give feedback.
-
I found a temporary fix for this. I am manually decoding by getting the raw token like this.
This works for me for now. |
Beta Was this translation helpful? Give feedback.
-
After hours of trying to figure it out, what we found was a stackoverflow post saying to explicitly set the cookie name. We also explicitly set the secret which we haven't even tested if it works without that, but now we know this is what's working for us both locally and on deployment. return await getToken({
req,
secret: process.env.NEXTAUTH_SECRET,
cookieName: "next-auth.session-token"
}); |
Beta Was this translation helpful? Give feedback.
-
Thank you all, you are life saver🥹 |
Beta Was this translation helpful? Give feedback.
-
Same issue! I struggled to resolve this issue for a few hours. In my case, there are two different cookies version for production and development. Here is my code. const token = await getToken({
req: request,
secret: process.env.NEXTAUTH_SECRET,
cookieName:
process.env.NODE_ENV === 'production'
? '__Secure-next-auth.session-token'
: 'next-auth.session-token',
}); Thanks for this posting. |
Beta Was this translation helpful? Give feedback.
-
When using Typscript, the salt property is also required. Make sure to set it to the same value as well. const cookieKey = process.env.NODE_ENV === 'production' ? '__Secure-authjs.session-token' : 'authjs.session-token';
const token = await getToken({
req,
secret,
salt: cookieKey,
cookieName: cookieKey
}); |
Beta Was this translation helpful? Give feedback.
-
Something that works in my local development does not work when I publish to vercel on a preview. The issue is the fact that getToken returns null in the middleware function of withAuth. In the file https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/next/middleware.ts , the getToken method which is necessary for the functionality of withAuth returns null. The getToken method is here:
next-auth/packages/next-auth/src/jwt/index.ts
Line 67 in b3b8d4b
which would normally work local will always give authorized as false in deployment. I'm not sure where this error comes from. Any help would be appreciated
Beta Was this translation helpful? Give feedback.
All reactions