[GH-ISSUE #7352] oauth_state cookie is not removed after consumption #10790

Closed
opened 2026-04-13 07:08:33 -05:00 by GiteaMirror · 6 comments
Owner

Originally created by @cflee on GitHub (Jan 14, 2026).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/7352

Originally assigned to: @bytaesu on GitHub.

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

With account.storeStateStrategy: "cookie" and an OAuth provider, go through a successful login flow and observe the Set-Cookie: headers along the way.

Current vs. Expected behavior

Current behavior: the __Secure-better-auth.oauth_state cookie is still there

Expected behavior: the __Secure-better-auth.oauth_state cookie is deleted/cleared after login completes and the cookie is consumed.

Observations:

Upon the POST to /api/auth/sign-in/social, this cookie is set:

__Secure-better-auth.oauth_state=...redacted...; Max-Age=600000; Path=/; HttpOnly; Secure; SameSite=Lax

Upon the GET to /api/auth/callback/providername, this cookie is set:

__Secure-better-auth.oauth_state=; Max-Age=0; Secure

However the browser would not respect this deletion of the cookie as the Path does not match. The Path should also be set when clearing the cookie in order to successfully clear it. This is done in the codebase on some code paths, but not for this oauth_state cookie.

What version of Better Auth are you using?

1.4.10

System info

{
  "system": {
    "platform": "linux",
    "arch": "arm64",
    "version": "#51-Ubuntu SMP PREEMPT_DYNAMIC Sat Nov  9 18:03:35 UTC 2024",
    "release": "6.8.0-50-generic",
    "cpuCount": 3,
    "cpuModel": "unknown",
    "totalMemory": "9.69 GB",
    "freeMemory": "5.64 GB"
  },
  "node": {
    "version": "v24.10.0",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "11.6.1"
  },
  "frameworks": [
    {
      "name": "express",
      "version": "^5.2.1"
    },
    {
      "name": "astro",
      "version": "^5.16.6"
    }
  ],
  "databases": null,
  "betterAuth": {
    "version": "^1.4.10",
    "config": {
      "baseURL": "http://localhost:4321",
      "secret": "[REDACTED]",
      "session": {
        "cookieCache": {
          "enabled": true,
          "maxAge": 172800,
          "strategy": "jwe",
          "refreshCache": true
        }
      },
      "account": {
        "storeStateStrategy": "cookie",
        "storeAccountCookie": true
      },
      "plugins": [
        {
          "name": "custom-social-providers",
          "config": {
            "id": "custom-social-providers"
          }
        },
        {
          "name": "oauth-proxy",
          "config": {
            "id": "oauth-proxy",
            "options": {},
            "endpoints": {},
            "hooks": {
              "before": [
                {},
                {},
                {}
              ],
              "after": [
                {},
                {},
                {}
              ]
            }
          }
        }
      ]
    }
  }
}

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)


Additional context

I was using a Microsoft Entra ID social provider but I don't think it affects this, the key thing is that it is in the stateless mode config without database.

Originally created by @cflee on GitHub (Jan 14, 2026). Original GitHub issue: https://github.com/better-auth/better-auth/issues/7352 Originally assigned to: @bytaesu on GitHub. ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce With `account.storeStateStrategy: "cookie"` and an OAuth provider, go through a successful login flow and observe the `Set-Cookie:` headers along the way. ### Current vs. Expected behavior Current behavior: the `__Secure-better-auth.oauth_state` cookie is still there Expected behavior: the `__Secure-better-auth.oauth_state` cookie is deleted/cleared after login completes and the cookie is consumed. Observations: Upon the POST to `/api/auth/sign-in/social`, this cookie is set: ``` __Secure-better-auth.oauth_state=...redacted...; Max-Age=600000; Path=/; HttpOnly; Secure; SameSite=Lax ``` Upon the GET to `/api/auth/callback/providername`, this cookie is set: ``` __Secure-better-auth.oauth_state=; Max-Age=0; Secure ``` However the browser would not respect this deletion of the cookie as the `Path` does not match. The `Path` should also be set when clearing the cookie in order to successfully clear it. This is done in the codebase on some code paths, but not for this `oauth_state` cookie. ### What version of Better Auth are you using? 1.4.10 ### System info ```bash { "system": { "platform": "linux", "arch": "arm64", "version": "#51-Ubuntu SMP PREEMPT_DYNAMIC Sat Nov 9 18:03:35 UTC 2024", "release": "6.8.0-50-generic", "cpuCount": 3, "cpuModel": "unknown", "totalMemory": "9.69 GB", "freeMemory": "5.64 GB" }, "node": { "version": "v24.10.0", "env": "development" }, "packageManager": { "name": "npm", "version": "11.6.1" }, "frameworks": [ { "name": "express", "version": "^5.2.1" }, { "name": "astro", "version": "^5.16.6" } ], "databases": null, "betterAuth": { "version": "^1.4.10", "config": { "baseURL": "http://localhost:4321", "secret": "[REDACTED]", "session": { "cookieCache": { "enabled": true, "maxAge": 172800, "strategy": "jwe", "refreshCache": true } }, "account": { "storeStateStrategy": "cookie", "storeAccountCookie": true }, "plugins": [ { "name": "custom-social-providers", "config": { "id": "custom-social-providers" } }, { "name": "oauth-proxy", "config": { "id": "oauth-proxy", "options": {}, "endpoints": {}, "hooks": { "before": [ {}, {}, {} ], "after": [ {}, {}, {} ] } } } ] } } } ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript ``` ### Additional context I was using a Microsoft Entra ID social provider but I don't think it affects this, the key thing is that it is in the stateless mode config without database.
GiteaMirror added the locked label 2026-04-13 07:08:34 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Jan 14, 2026):

It looks like you're encountering an issue where the __Secure-better-auth.oauth_state cookie is not being properly removed after its intended use in the OAuth flow.

From the details provided, it seems that the cookie is set correctly upon the OAuth authentication step, but it fails to be deleted afterwards because the path doesn't match when you attempt to clear it. This is a known issue as indicated in the referenced open issue regarding cookie management in various OAuth flows, particularly with state handling [1][2].

Next Steps:

  1. Ensure Path Consistency: Check to confirm that the path parameter is included when you're attempting to remove the oauth_state cookie. All cookies set with specific paths must be deleted using the same path in the cookie clear request.

  2. Follow the Community Discussion: This topic has been raised in community discussions, and solutions or workarounds may already be underway. Keeping an eye on the progress of the relevant issues can lead to updates on fixes.

  3. Consider Reaching out for Assistance: If the problem persists, tagging @bekacru and @ping__ on your post may help you get direct insights from Better Auth team members who can address your concerns more effectively.

Would you like more information on a specific aspect of handling OAuth flow or cookie management?

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3748366142 --> @better-auth-agent[bot] commented on GitHub (Jan 14, 2026): It looks like you're encountering an issue where the `__Secure-better-auth.oauth_state` cookie is not being properly removed after its intended use in the OAuth flow. From the details provided, it seems that the cookie is set correctly upon the OAuth authentication step, but it fails to be deleted afterwards because the path doesn't match when you attempt to clear it. This is a known issue as indicated in the referenced open issue regarding cookie management in various OAuth flows, particularly with state handling [[1]](https://github.com/better-auth/better-auth/issues/6847)[[2]](https://github.com/better-auth/better-auth/pull/6933). ### Next Steps: 1. **Ensure Path Consistency:** Check to confirm that the path parameter is included when you're attempting to remove the `oauth_state` cookie. All cookies set with specific paths must be deleted using the same path in the cookie clear request. 2. **Follow the Community Discussion:** This topic has been raised in community discussions, and solutions or workarounds may already be underway. Keeping an eye on the progress of the relevant issues can lead to updates on fixes. 3. **Consider Reaching out for Assistance:** If the problem persists, tagging @bekacru and @ping__ on your post may help you get direct insights from Better Auth team members who can address your concerns more effectively. Would you like more information on a specific aspect of handling OAuth flow or cookie management? _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) • [Discord](https://discord.gg/better-auth) • [GitHub](https://github.com/better-auth/better-auth) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/better-auth) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/better-auth/better-auth)
Author
Owner

@cflee commented on GitHub (Jan 14, 2026):

@better-auth-agent the linked issue and PR are only applicable to Expo. This issue affects other usage.

<!-- gh-comment-id:3748374484 --> @cflee commented on GitHub (Jan 14, 2026): @better-auth-agent the linked issue and PR are only applicable to Expo. This issue affects other usage.
Author
Owner

@better-auth-agent[bot] commented on GitHub (Jan 14, 2026):

It looks like you are facing an issue where the __Secure-better-auth.oauth_state cookie is not being cleared after the OAuth login process completes. Based on the behavior observed, it seems that the cookie is set correctly during the login flow, but it's not being properly cleared afterward due to a mismatch in the Path attribute.

Next Steps:

  1. Check Path Consistency: Ensure that when you are attempting to clear the oauth_state cookie, the Path attribute set during its deletion matches the Path used when it was created. This is crucial for the browser to successfully delete the cookie.

  2. Review Cookie Settings: You might want to verify other cookie configurations such as SameSite and Secure attributes, as these can also affect cookie behavior across various browsers.

  3. Update Version: Since you're using version 1.4.10, check if there are any updates or patches that address this specific cookie handling issue. Sometimes enhancements or bug fixes are introduced in minor releases.

If these steps don't resolve the issue, feel free to tag @bekacru or @ping__ on Discord for further assistance from the Better Auth team.

Engaging Question: Have you already tried using a different cookie strategy, like setting it to "database," to see if that resolves the issue temporarily?

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3748375416 --> @better-auth-agent[bot] commented on GitHub (Jan 14, 2026): It looks like you are facing an issue where the `__Secure-better-auth.oauth_state` cookie is not being cleared after the OAuth login process completes. Based on the behavior observed, it seems that the cookie is set correctly during the login flow, but it's not being properly cleared afterward due to a mismatch in the `Path` attribute. **Next Steps:** 1. **Check Path Consistency**: Ensure that when you are attempting to clear the `oauth_state` cookie, the `Path` attribute set during its deletion matches the `Path` used when it was created. This is crucial for the browser to successfully delete the cookie. 2. **Review Cookie Settings**: You might want to verify other cookie configurations such as `SameSite` and `Secure` attributes, as these can also affect cookie behavior across various browsers. 3. **Update Version**: Since you're using version **1.4.10**, check if there are any updates or patches that address this specific cookie handling issue. Sometimes enhancements or bug fixes are introduced in minor releases. If these steps don't resolve the issue, feel free to tag @bekacru or @ping__ on Discord for further assistance from the Better Auth team. **Engaging Question**: Have you already tried using a different cookie strategy, like setting it to "database," to see if that resolves the issue temporarily? <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) • [Discord](https://discord.gg/better-auth) • [GitHub](https://github.com/better-auth/better-auth) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/better-auth) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/better-auth/better-auth)
Author
Owner

@bytaesu commented on GitHub (Jan 14, 2026):

Hi @cflee, I'm checking this!

<!-- gh-comment-id:3748498323 --> @bytaesu commented on GitHub (Jan 14, 2026): Hi @cflee, I'm checking this!
Author
Owner

@cflee commented on GitHub (Jan 14, 2026):

Looking at the codebase, it might affect all the cases of calling setCookie() with options only of { maxAge: 0 } (without spreading some other options/attributes), where the cookie was originally created from createAuthCookie(). So potentially not just for this cookie.

<!-- gh-comment-id:3748580951 --> @cflee commented on GitHub (Jan 14, 2026): Looking at the codebase, it might affect all the cases of calling `setCookie()` with options only of `{ maxAge: 0 }` (without spreading some other options/attributes), where the cookie was originally created from `createAuthCookie()`. So potentially not just for this cookie.
Author
Owner

@bytaesu commented on GitHub (Jan 14, 2026):

Thanks @cflee, this seems valid. I'll fix this.

<!-- gh-comment-id:3748647314 --> @bytaesu commented on GitHub (Jan 14, 2026): Thanks @cflee, this seems valid. I'll fix this.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#10790