Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deep-merge cookies options #9386

Merged
merged 7 commits into from
Dec 14, 2023

Conversation

yordis
Copy link
Contributor

@yordis yordis commented Dec 13, 2023

Context

Recently we had to manually configure the cookies in order to get our API service to authenticate with the NextAuth
cookie session token. Because the API lives under api.example.com and the NextJS app lives under example.com we had
to set the domain property of the cookie to .example.com in order to get the cookie to be sent to the API.

Here is the relevant code:

export const authOptions = {
  cookies: {
    sessionToken: {
      name: process.env.NODE_ENV === 'production' ? '__Secure-next-auth.session-token' : 'next-auth.session-token',
      options: {
        httpOnly: true,
        sameSite: 'lax',
        path: '/',
        secure: process.env.NODE_ENV === 'production',
        // this is the important part
        domain: process.env.NEXTAUTH_SESSION_TOKEN_COOKIE_DOMAIN,
      },
    },
    // removed the other cookies for brevity
  },
} satisfies NextAuthOptions;

Concern

In order to accomplish this, we have to overwrite the entire cookie configuration. This means that we have to
manually set the httpOnly, sameSite, path, secure, etc. properties. This is not ideal because if the NextAuth
team ever changes the default values for these properties, we will not get the benefit of those changes.
Or if new sensible defaults are added, we will not get those either.

It is critical to have secured and sensible defaults for these properties.

Also notice the error-prone nature of this code since it requires to understand interanlly how NextAuth works and how
cookies work in general. Adding __Secure-, __Host- (which middlewares in other languages relies on), or extremely
critical, make sure secure is set to true in production.

Breaking Change?

Since we must overwrite the entire cookie configuration as of today, this should not be a breaking change as far as I
can tell.

Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
Copy link

vercel bot commented Dec 13, 2023

@yordis is attempting to deploy a commit to the authjs Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

vercel bot commented Dec 13, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
auth-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 14, 2023 2:42am
2 Ignored Deployments
Name Status Preview Comments Updated (UTC)
auth-docs-nextra ⬜️ Ignored (Inspect) Visit Preview Dec 14, 2023 2:42am
next-auth-docs ⬜️ Ignored (Inspect) Visit Preview Dec 14, 2023 2:42am

@github-actions github-actions bot added the core Refers to `@auth/core` label Dec 13, 2023
@yordis yordis marked this pull request as ready for review December 13, 2023 22:42
Copy link
Member

@balazsorban44 balazsorban44 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! First I was hesitant, but this keeps the current defaults, so it should be safe.

However, some other tweaks were needed, which I applied.

packages/core/src/types.ts Outdated Show resolved Hide resolved
packages/core/src/lib/init.ts Outdated Show resolved Hide resolved
packages/core/src/lib/utils/cookie.ts Outdated Show resolved Hide resolved
packages/core/src/lib/utils/cookie.ts Outdated Show resolved Hide resolved
packages/core/test/index.test.ts Outdated Show resolved Hide resolved
packages/core/test/index.test.ts Outdated Show resolved Hide resolved
@balazsorban44 balazsorban44 merged commit 17c4426 into nextauthjs:main Dec 14, 2023
9 of 10 checks passed
@yordis yordis deleted the merge-cookies-options branch January 6, 2024 22:00
@yordis
Copy link
Contributor Author

yordis commented Jan 6, 2024

Hey @balazsorban44 is there any opportunity to release this feature 🙏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Refers to `@auth/core`
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants