[PR #7206] [CLOSED] fix(bearer): certain sign-in endpoints won't give bearer token #7144

Closed
opened 2026-03-13 13:25:38 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7206
Author: @subasshrestha
Created: 1/8/2026
Status: Closed

Base: canaryHead: canary


📝 Commits (8)

  • 0c9d91a fix(bearer): social sign-in endpoints won't give bearer token
  • 5ebc283 Merge branch 'canary' into canary
  • 69f8c6b chore: lint
  • 68ac22f fix: update suggestions
  • f075fe4 Update
  • ee54d5c Merge branch 'canary' into canary
  • f68f6c6 fix: remove redundant URL creation for location redirection
  • e0a4bb0 Merge branch 'canary' into canary

📊 Changes

7 files changed (+275 additions, -41 deletions)

View changed files

📝 demo/nextjs/lib/auth-client.ts (+2 -0)
📝 docs/content/docs/plugins/bearer.mdx (+60 -40)
📝 packages/better-auth/src/client/plugins/index.ts (+1 -0)
📝 packages/better-auth/src/plugins/bearer/bearer.test.ts (+119 -1)
packages/better-auth/src/plugins/bearer/client.ts (+52 -0)
📝 packages/better-auth/src/plugins/bearer/index.ts (+35 -0)
📝 packages/better-auth/src/plugins/generic-oauth/generic-oauth.test.ts (+6 -0)

📄 Description

Fixes 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 page will cause the authClient not pick up on the bearer token. This PR addresses this issue.

Solution

Server-Side Changes

  • Added logic to detect redirect responses (via location header)
  • Appends set-auth-token query parameter to redirect URLs
  • Safely handles invalid URLs with try-catch

Client-Side Enhancement

  • bearerClient() plugin for automatic token handling
  • Automatically extracts token from URL parameters (set-auth-token)
  • Stores token in localStorage (configurable key)
  • Also, Captures token from response headers
  • Cleans up URL after extracting token

Documentation Updates

  • Simplified setup instructions
  • Added bearerClient() plugin usage examples
  • Added configuration options for both server and client

Breaking Changes

None - this is a backward-compatible enhancement.


Summary by cubic

Fixes missing bearer tokens for social sign-ins and magic link flows by appending the token to same-origin (or trusted) redirect URLs and adding a client plugin to auto-capture and store it. This makes Bearer auth work consistently across all sign-in endpoints.

  • Bug Fixes

    • Detect redirect responses and append set-auth-token to the Location header for same-origin or trusted origins.
    • Safely handle invalid redirect URLs.
    • Added tests to verify token is included in social callback redirects and usable for session requests.
  • New Features

    • Added bearerClient() to automatically read tokens from URL params and response headers, store them in localStorage, and clean the URL.
    • Configurable localStorage key for token storage.
    • Server option: trustedRedirectOrigins to allow specific cross-origin redirects to receive tokens.
    • Exported plugin via client plugins index, wired into demo client.
    • Updated docs with simpler setup, bearerClient() usage, and trustedRedirectOrigins guidance.

Written for commit e0a4bb073a. Summary will update on new commits.


🔄 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/7206 **Author:** [@subasshrestha](https://github.com/subasshrestha) **Created:** 1/8/2026 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `canary` --- ### 📝 Commits (8) - [`0c9d91a`](https://github.com/better-auth/better-auth/commit/0c9d91a96b406fe3487189a1fd5e4b8241eba779) fix(bearer): social sign-in endpoints won't give bearer token - [`5ebc283`](https://github.com/better-auth/better-auth/commit/5ebc2835f4294a1e7ed1b6cd2b9a9cf5d00d5735) Merge branch 'canary' into canary - [`69f8c6b`](https://github.com/better-auth/better-auth/commit/69f8c6b39597a50a01755b5721973399051bd86e) chore: lint - [`68ac22f`](https://github.com/better-auth/better-auth/commit/68ac22fff180288e30a6478b6ccc12b9f0b64bbc) fix: update suggestions - [`f075fe4`](https://github.com/better-auth/better-auth/commit/f075fe47213d1cd5e4f4675ce1710ddc9a7b6b29) Update - [`ee54d5c`](https://github.com/better-auth/better-auth/commit/ee54d5caedc62410b3811d34de42e988464d95b7) Merge branch 'canary' into canary - [`f68f6c6`](https://github.com/better-auth/better-auth/commit/f68f6c6e41f130668118adb0938419293955e845) fix: remove redundant URL creation for location redirection - [`e0a4bb0`](https://github.com/better-auth/better-auth/commit/e0a4bb073accb9c7c31ac6429f448bf9ee5350f3) Merge branch 'canary' into canary ### 📊 Changes **7 files changed** (+275 additions, -41 deletions) <details> <summary>View changed files</summary> 📝 `demo/nextjs/lib/auth-client.ts` (+2 -0) 📝 `docs/content/docs/plugins/bearer.mdx` (+60 -40) 📝 `packages/better-auth/src/client/plugins/index.ts` (+1 -0) 📝 `packages/better-auth/src/plugins/bearer/bearer.test.ts` (+119 -1) ➕ `packages/better-auth/src/plugins/bearer/client.ts` (+52 -0) 📝 `packages/better-auth/src/plugins/bearer/index.ts` (+35 -0) 📝 `packages/better-auth/src/plugins/generic-oauth/generic-oauth.test.ts` (+6 -0) </details> ### 📄 Description Fixes 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 page will cause the authClient not pick up on the bearer token. This PR addresses this issue. ## Solution ### Server-Side Changes - Added logic to detect redirect responses (via `location` header) - Appends `set-auth-token` query parameter to redirect URLs - Safely handles invalid URLs with try-catch ### Client-Side Enhancement - `bearerClient()` plugin for automatic token handling - Automatically extracts token from URL parameters (`set-auth-token`) - Stores token in localStorage (configurable key) - Also, Captures token from response headers - Cleans up URL after extracting token ### Documentation Updates - Simplified setup instructions - Added `bearerClient()` plugin usage examples - Added configuration options for both server and client ## Breaking Changes None - this is a backward-compatible enhancement. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes missing bearer tokens for social sign-ins and magic link flows by appending the token to same-origin (or trusted) redirect URLs and adding a client plugin to auto-capture and store it. This makes Bearer auth work consistently across all sign-in endpoints. - **Bug Fixes** - Detect redirect responses and append set-auth-token to the Location header for same-origin or trusted origins. - Safely handle invalid redirect URLs. - Added tests to verify token is included in social callback redirects and usable for session requests. - **New Features** - Added bearerClient() to automatically read tokens from URL params and response headers, store them in localStorage, and clean the URL. - Configurable localStorage key for token storage. - Server option: trustedRedirectOrigins to allow specific cross-origin redirects to receive tokens. - Exported plugin via client plugins index, wired into demo client. - Updated docs with simpler setup, bearerClient() usage, and trustedRedirectOrigins guidance. <sup>Written for commit e0a4bb073accb9c7c31ac6429f448bf9ee5350f3. Summary will update on new commits.</sup> <!-- 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 13:25:38 -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#7144