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

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/4330
Author: @ping-maxwell
Created: 8/31/2025
Status: Merged
Merged: 9/2/2025
Merged by: @himself65

Base: canaryHead: fix/bearer/cant-get-token


📝 Commits (5)

  • f3806b4 fix(bearer): certain sign-in endpoints won't give bearer token v2
  • 10115d9 Merge branch 'canary' into fix/bearer/cant-get-token
  • a39999f chore: lint
  • 2c68a2a fix
  • 62c5756 Merge branch 'canary' into fix/bearer/cant-get-token

📊 Changes

6 files changed (+140 additions, -2 deletions)

View changed files

📝 docs/content/docs/plugins/bearer.mdx (+16 -1)
📝 packages/better-auth/src/client/plugins/index.ts (+1 -0)
📝 packages/better-auth/src/plugins/bearer/bearer.test.ts (+11 -0)
packages/better-auth/src/plugins/bearer/client.ts (+27 -0)
📝 packages/better-auth/src/plugins/bearer/index.ts (+80 -1)
📝 packages/better-auth/src/test-utils/test-instance.ts (+5 -0)

📄 Description

The first PR was merged by accident (I assume it's by accident) by Alex, then reverted. So this is the same PR plus with new changes based on Alex's change request

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 token after social logins and magic links by adding a redirect-safe handoff between server and client. Adds a lightweight client plugin and an internal endpoint to recover the token after OAuth redirects.

  • Bug Fixes

    • Server: when a sign-in response includes a Location header, sets a temporary confirmation cookie and exposes a new GET /get-bearer-token endpoint that reads the session cookie, sets the set-auth-token header, and clears the temporary cookie. Adds an option to customize the cookie name.
    • Client: adds bearerClient() plugin that, on startup, detects the confirmation cookie and calls /get-bearer-token so the auth client receives the token via onSuccess.
    • DX: exports bearerClient from client/plugins and updates docs with the client setup and cookieName option.
  • Migration

    • Add bearerClient() to your createAuthClient plugins array:
      • import { bearerClient } from "better-auth/client/plugins"
      • plugins: [bearerClient()]

🔄 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/4330 **Author:** [@ping-maxwell](https://github.com/ping-maxwell) **Created:** 8/31/2025 **Status:** ✅ Merged **Merged:** 9/2/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/bearer/cant-get-token` --- ### 📝 Commits (5) - [`f3806b4`](https://github.com/better-auth/better-auth/commit/f3806b4c11072ef53730f347d1e5465d9cce0382) fix(bearer): certain sign-in endpoints won't give bearer token v2 - [`10115d9`](https://github.com/better-auth/better-auth/commit/10115d9aa3a39b5f903991d081032a2c38b7fd59) Merge branch 'canary' into fix/bearer/cant-get-token - [`a39999f`](https://github.com/better-auth/better-auth/commit/a39999f23953ab9810aa54509e2ebc1b5252ff19) chore: lint - [`2c68a2a`](https://github.com/better-auth/better-auth/commit/2c68a2a2cc23866997a9ea3a97cadc1dc5fe2dfe) fix - [`62c5756`](https://github.com/better-auth/better-auth/commit/62c575601f6aeaa335ebcc46007aacce0c169b3e) Merge branch 'canary' into fix/bearer/cant-get-token ### 📊 Changes **6 files changed** (+140 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/plugins/bearer.mdx` (+16 -1) 📝 `packages/better-auth/src/client/plugins/index.ts` (+1 -0) 📝 `packages/better-auth/src/plugins/bearer/bearer.test.ts` (+11 -0) ➕ `packages/better-auth/src/plugins/bearer/client.ts` (+27 -0) 📝 `packages/better-auth/src/plugins/bearer/index.ts` (+80 -1) 📝 `packages/better-auth/src/test-utils/test-instance.ts` (+5 -0) </details> ### 📄 Description The first PR was merged by accident (I assume it's by accident) by Alex, then reverted. So this is the same PR plus with new changes based on Alex's change request ## 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 token after social logins and magic links by adding a redirect-safe handoff between server and client. Adds a lightweight client plugin and an internal endpoint to recover the token after OAuth redirects. - **Bug Fixes** - Server: when a sign-in response includes a Location header, sets a temporary confirmation cookie and exposes a new GET /get-bearer-token endpoint that reads the session cookie, sets the set-auth-token header, and clears the temporary cookie. Adds an option to customize the cookie name. - Client: adds bearerClient() plugin that, on startup, detects the confirmation cookie and calls /get-bearer-token so the auth client receives the token via onSuccess. - DX: exports bearerClient from client/plugins and updates docs with the client setup and cookieName option. - **Migration** - Add bearerClient() to your createAuthClient plugins array: - import { bearerClient } from "better-auth/client/plugins" - plugins: [bearerClient()] <!-- 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:18:22 -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#5315