I’m encountering issues with authentication in my Next.js application using Next-auth. After deploying to Vercel, some users are unable to sign in, and the network error shows a 431: REQUEST_HEADER_FIELDS_TOO_LARGE
error, indicating that the JWT cookie size is too large.
I initially addressed this by setting NODE_OPTIONS='--max-http-header-size=24576 next dev
locally, which seemed to fix the issue during development. However, upon deploying to Vercel, the problem persisted, even after setting NODE_OPTIONS='--max-http-header-size=24576 next build
.
Additionally, after making these changes, some users began reporting an Error Unexpected token < in JSON at position 0
when attempting to sign in. This error wasn’t occurring during local development or on a new deployment elsewhere.
Here is the relevant portion of my Next-auth configuration for JWT handling:
jwt: {
secret: process.env.JWT_SECRET,
encode: async ({ secret, token }) => {
const payload = { id: token?.id };
const encodedToken = jwt.sign(payload, secret, { algorithm: 'HS256' });
return encodedToken;
},
decode: async ({ secret, token }) => {
if (!token) {
throw new Error('Token is undefined');
}
try {
const decodedToken = jwt.verify(token, secret, { algorithms: ['HS256'] });
return decodedToken;
} catch (error) {
throw new Error('Failed to decode token');
}
},
},
My Questions:
How can I ensure that the JWT cookie size remains manageable across different users without compromising security or functionality?
What might be causing the 431 error to occur selectively for certain users despite varying data stored in the database?
How should I approach resolving the Unexpected token < in JSON at position 0 error specifically on Vercel, considering it does not occur locally or on other deployments?
Any insights or suggestions on optimizing JWT handling in Next-auth on Vercel would be greatly appreciated. Thank you
ps
https://codesandbox.io/p/sandbox/next-auth-2fpx8g?file=%2Fauth.ts
I uploaded auth.ts (nextauth) and signin.ts(signin API route) file
1