0

I’m using next-auth with the JWT session strategy and running into a problem with cookie size when storing multiple API tokens.

Here’s my JWT callback:

async jwt({ token, user }) { if (user) { token.agentInfo = user.agentInfo; } const cognitoTokens = await manageTokens(token as Record<string, Token>); token = { ...token, ...cognitoTokens }; return token; } 

In my case, manageTokens merges three different API tokens (Cognito + other APIs) into the JWT payload. but in __Secure-next-auth.session-token cookie becomes too large and NextAuth splits it into multiple cookies (.0, .1, .2).

Why this is a problem

  • Some APIs start returning 431 Request Header Fields Too Large because the cookies in the request headers are too big
  • I don’t actually use the cookie-based JWT anywhere in our app.
  • I only rely on the session token inside next-auth.
  • Because the JWT grows with multiple tokens, the cookie approach creates unnecessary complexity.

Questions

  • Is there a way to disable cookie storage of the JWT when using the jwt session strategy and only keep it in memory/server-side?
  • If not, what’s the recommended pattern for handling multiple tokens without hitting cookie size limits?
  • Should i be moving to a server-side session store (e.g., PrismaAdapter + DB/Redis)?
  • Or should NextAuth provide an option to avoid serializing everything into cookies?

I tried only non-serializing the token it worked but sometimes it does not work it is very weird

1 Answer 1

0

You can store token inside Redis or backend in memory and you only need to store sessionId and send backend session to frontend. So you do not need store large JWT in a cookie or in other storages.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.