fix(auth): resolve NextAuth session cookie decryption in production #682
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a critical authentication issue where users were redirected to the login page after successful authentication in production environments (Kubernetes with Apache proxy), while development mode worked correctly.
Root Cause
The middleware's
getToken()function fromnext-auth/jwtwas unable to decrypt the session cookie in production because it requires explicit parameters to match how the token was encrypted:secretsecureCookie__Secure-for HTTPS)cookieNamesaltIn development (
http://localhost:3000), the cookie name isauthjs.session-tokenand defaults worked. In production (https://...), the cookie name is__Secure-authjs.session-tokenand explicit parameters are required.Changes
Authentication middleware (
src/middleware.ts):getToken()parameters for proper cookie decryption in HTTPS environmentsNEXTAUTH_URLasync)Version bump (
package.json):39.0.2Why
auth()wrapper wasn't usedThe recommended Auth.js v5 pattern using the
auth()wrapper failed due to Edge Runtime incompatibility - the import chain pulls inreflect-metadata(useseval) andnode:streamwhich are not allowed in Edge Runtime where middleware runs.Test Plan
npm run dev)