[PR #4123] [MERGED] fix(bearer): certain sign-in endpoints won't give bearer token #5201

Closed
opened 2026-03-13 12:13:52 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/4123
Author: @ping-maxwell
Created: 8/21/2025
Status: Merged
Merged: 8/26/2025
Merged by: @himself65

Base: canaryHead: fix/bearer/not-working-on-social-or-ml


📝 Commits (10+)

  • ad26e58 fix(bearer): Certain sign-in endpoints won't give bearer token
  • e0982af chore: cleanup
  • 4c6c2b0 add: docs
  • 2f8138a update
  • 6253bba Merge branch 'canary' into fix/bearer/not-working-on-social-or-ml
  • ad4bab1 chore: lint
  • 53e2686 Merge branch 'canary' into fix/bearer/not-working-on-social-or-ml
  • 18f1675 add: cookie options
  • 2e12fe2 Merge branch 'canary' into fix/bearer/not-working-on-social-or-ml
  • b40652c fix: CI

📊 Changes

6 files changed (+112 additions, -3 deletions)

View changed files

📝 docs/content/docs/plugins/bearer.mdx (+12 -1)
📝 packages/better-auth/src/adapters/kysely-adapter/test/state.txt (+1 -1)
📝 packages/better-auth/src/client/plugins/index.ts (+1 -0)
packages/better-auth/src/plugins/bearer/client.ts (+17 -0)
📝 packages/better-auth/src/plugins/bearer/index.ts (+76 -1)
📝 packages/better-auth/src/test-utils/test-instance.ts (+5 -0)

📄 Description

Closes https://github.com/better-auth/better-auth/issues/3082

What's this PR for?

Normal sign-in endpoints will return a response which includes the set-auth-token header from the bearer plugin, however some sign-in endpoints such as social logins or magic links where the set-cookie response comes from a separate /api/auth page will cause the authClient not pick up on the bearer token. This PR addresses this issue.

How does it address it?

We can address this issue by checking if the set-cookie response in the hook also contains a location header thus meaning it will be redirected. (For example social logins where the user is redirected to /callback which will give a set-cookie as well as a location header) If we find out that it does include a location value, then we can set a cookie called bearer-token-confirmation which is just true. Then, on the client bearer plugin, on-start-up it will check if that cookie exists, and if so, hit a newly /get-bearer-value endpoint. This endpoint will then remove the bearer-token-confirmation cookie and then return the bearer token by just grabbing it from the session cookie value. Once it's returned, the authClient's onSuccess global hook would pickup on that response and thus the new bearer token is returned.

An example flow will look like:
image

Breaking Changes / Migrations?

Nothing will break, however the need to add bearerClient plugin on the authClient will be required in order for social logins/magic link or other similar sign-ins to work.


Summary by cubic

Fixes missing bearer tokens after social login and magic link sign-ins that redirect, so clients reliably receive a token on startup. Uses a temporary cookie and a new endpoint to recover the token after redirects.

  • Bug Fixes

    • Add bearer client plugin that, on load, checks for a bearer-token cookie and calls /get-bearer-token.
    • Introduce GET /get-bearer-token to read the session cookie, set set-auth-token, and clear the bearer-token cookie.
    • On sign-in responses that include Set-Cookie and a Location header (redirect flows), set a temporary bearer-token-confirmation=true cookie to trigger recovery.
  • Migration

    • Add bearerClient() to the auth client plugins to enable token recovery on startup.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/4123 **Author:** [@ping-maxwell](https://github.com/ping-maxwell) **Created:** 8/21/2025 **Status:** ✅ Merged **Merged:** 8/26/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/bearer/not-working-on-social-or-ml` --- ### 📝 Commits (10+) - [`ad26e58`](https://github.com/better-auth/better-auth/commit/ad26e5880d6b0d4e42d81f870441489319da75f2) fix(bearer): Certain sign-in endpoints won't give bearer token - [`e0982af`](https://github.com/better-auth/better-auth/commit/e0982afaafab42e91954bd0a5089c60cb347ecfd) chore: cleanup - [`4c6c2b0`](https://github.com/better-auth/better-auth/commit/4c6c2b01dfb1b4088e0915a472f1bb9bf96ad1b8) add: docs - [`2f8138a`](https://github.com/better-auth/better-auth/commit/2f8138a0c6536eacbd60cd49dd523e242dd15796) update - [`6253bba`](https://github.com/better-auth/better-auth/commit/6253bba320734677e76272f09955d507d48b51e9) Merge branch 'canary' into fix/bearer/not-working-on-social-or-ml - [`ad4bab1`](https://github.com/better-auth/better-auth/commit/ad4bab118af257698b6794369a2ac5c8530522f5) chore: lint - [`53e2686`](https://github.com/better-auth/better-auth/commit/53e2686126e1033ab485fc8904035b9832d9833e) Merge branch 'canary' into fix/bearer/not-working-on-social-or-ml - [`18f1675`](https://github.com/better-auth/better-auth/commit/18f1675bc8412c8c86aae2c736cbb24d179dd06b) add: cookie options - [`2e12fe2`](https://github.com/better-auth/better-auth/commit/2e12fe2f782ec71b0f56dc61de422bd9770dee6f) Merge branch 'canary' into fix/bearer/not-working-on-social-or-ml - [`b40652c`](https://github.com/better-auth/better-auth/commit/b40652c962ebc1f15deffd3d108f7565213fc28c) fix: CI ### 📊 Changes **6 files changed** (+112 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/plugins/bearer.mdx` (+12 -1) 📝 `packages/better-auth/src/adapters/kysely-adapter/test/state.txt` (+1 -1) 📝 `packages/better-auth/src/client/plugins/index.ts` (+1 -0) ➕ `packages/better-auth/src/plugins/bearer/client.ts` (+17 -0) 📝 `packages/better-auth/src/plugins/bearer/index.ts` (+76 -1) 📝 `packages/better-auth/src/test-utils/test-instance.ts` (+5 -0) </details> ### 📄 Description Closes https://github.com/better-auth/better-auth/issues/3082 ## What's this PR for? Normal sign-in endpoints will return a response which includes the `set-auth-token` header from the bearer plugin, however some sign-in endpoints such as social logins or magic links where the set-cookie response comes from a separate `/api/auth` page will cause the authClient not pick up on the bearer token. This PR addresses this issue. ## How does it address it? We can address this issue by checking if the `set-cookie` response in the hook also contains a `location` header thus meaning it will be redirected. (For example social logins where the user is redirected to /callback which will give a `set-cookie` as well as a `location` header) If we find out that it does include a location value, then we can set a cookie called `bearer-token-confirmation` which is just `true`. Then, on the client bearer plugin, on-start-up it will check if that cookie exists, and if so, hit a newly `/get-bearer-value` endpoint. This endpoint will then remove the `bearer-token-confirmation` cookie and then return the bearer token by just grabbing it from the session cookie value. Once it's returned, the authClient's `onSuccess` global hook would pickup on that response and thus the new bearer token is returned. An example flow will look like: <img width="775" height="797" alt="image" src="https://github.com/user-attachments/assets/2023919b-2318-440f-a799-b1805e4816a6" /> ## Breaking Changes / Migrations? Nothing will break, however the need to add `bearerClient` plugin on the authClient will be required in order for social logins/magic link or other similar sign-ins to work. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes missing bearer tokens after social login and magic link sign-ins that redirect, so clients reliably receive a token on startup. Uses a temporary cookie and a new endpoint to recover the token after redirects. - **Bug Fixes** - Add bearer client plugin that, on load, checks for a bearer-token cookie and calls /get-bearer-token. - Introduce GET /get-bearer-token to read the session cookie, set set-auth-token, and clear the bearer-token cookie. - On sign-in responses that include Set-Cookie and a Location header (redirect flows), set a temporary bearer-token-confirmation=true cookie to trigger recovery. - **Migration** - Add bearerClient() to the auth client plugins to enable token recovery on startup. <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 12:13:52 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#5201