[PR #8062] [CLOSED] fix(multi-session): ensure after hooks run on OAuth redirect #24618

Closed
opened 2026-04-15 22:28:29 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8062
Author: @Bekacru
Created: 2/19/2026
Status: Closed

Base: canaryHead: fix/issue-8060


📝 Commits (1)

  • 4a9bd1f fix(multi-session): ensure after hooks run on OAuth redirect

📊 Changes

1 file changed (+42 additions, -0 deletions)

View changed files

📝 packages/better-auth/src/api/to-auth-endpoints.ts (+42 -0)

📄 Description

Bug Report

OAuth sessions were not appearing in listDeviceSessions() because the multi-session cookie (*_multi-*) was not being set during OAuth login flows.

Root Cause

When the OAuth callback succeeds (packages/better-auth/src/api/routes/callback.ts), it calls setSessionCookie() then throw c.redirect(toRedirectTo).

The issue is in packages/better-auth/src/api/to-auth-endpoints.ts:

  • The endpoint runner's .catch() block only handles APIError
  • c.redirect() throws a Response object with a 3xx status code, not an APIError
  • The redirect response was being re-thrown, so after hooks never executed
  • The multi-session plugin's after hook sets the _multi-* cookie, but it never ran
  • Without this cookie, listDeviceSessions() doesn't include the OAuth session

The Fix

  1. Added isRedirectResponse() helper to detect Response objects with 3xx status codes
  2. Extended the catch block to also handle redirect responses like APIError
  3. After hooks now run on redirects, allowing the multi-session plugin to set its cookie
  4. Headers from after hooks (e.g., Set-Cookie) are merged into the final redirect response

Files Changed

  • packages/better-auth/src/api/to-auth-endpoints.ts - Handle redirect responses to ensure after hooks run

Testing

Please verify:

  1. Sign in via OAuth (Google/Apple/etc.)
  2. Call authClient.multiSession.listDeviceSessions()
  3. The OAuth session should now appear in the list
  4. Email/password sign-ins should continue to work as before

Fixes #8060

Fixes #8060


Summary by cubic

Ensures after hooks run on OAuth redirects so the multi-session cookie is set and OAuth sessions appear in listDeviceSessions. Fixes #8060.

  • Bug Fixes
    • Detect and handle 3xx redirect Response values with isRedirectResponse in to-auth-endpoints.
    • Run after hooks on redirects and merge their headers (including Set-Cookie) into the final response.
    • Multi-session plugin now sets the _multi-* cookie during OAuth flows; email/password flows unchanged.

Written for commit 4a9bd1fd62. 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/8062 **Author:** [@Bekacru](https://github.com/Bekacru) **Created:** 2/19/2026 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `fix/issue-8060` --- ### 📝 Commits (1) - [`4a9bd1f`](https://github.com/better-auth/better-auth/commit/4a9bd1fd62856fb7369a000670376cce1719f655) fix(multi-session): ensure after hooks run on OAuth redirect ### 📊 Changes **1 file changed** (+42 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/to-auth-endpoints.ts` (+42 -0) </details> ### 📄 Description ## Bug Report OAuth sessions were not appearing in `listDeviceSessions()` because the multi-session cookie (`*_multi-*`) was not being set during OAuth login flows. ## Root Cause When the OAuth callback succeeds (packages/better-auth/src/api/routes/callback.ts), it calls `setSessionCookie()` then `throw c.redirect(toRedirectTo)`. The issue is in `packages/better-auth/src/api/to-auth-endpoints.ts`: - The endpoint runner's `.catch()` block only handles `APIError` - `c.redirect()` throws a `Response` object with a 3xx status code, not an `APIError` - The redirect response was being re-thrown, so after hooks never executed - The multi-session plugin's after hook sets the `_multi-*` cookie, but it never ran - Without this cookie, `listDeviceSessions()` doesn't include the OAuth session ## The Fix 1. Added `isRedirectResponse()` helper to detect Response objects with 3xx status codes 2. Extended the catch block to also handle redirect responses like APIError 3. After hooks now run on redirects, allowing the multi-session plugin to set its cookie 4. Headers from after hooks (e.g., `Set-Cookie`) are merged into the final redirect response ## Files Changed - `packages/better-auth/src/api/to-auth-endpoints.ts` - Handle redirect responses to ensure after hooks run ## Testing Please verify: 1. Sign in via OAuth (Google/Apple/etc.) 2. Call `authClient.multiSession.listDeviceSessions()` 3. The OAuth session should now appear in the list 4. Email/password sign-ins should continue to work as before Fixes #8060 Fixes #8060 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Ensures after hooks run on OAuth redirects so the multi-session cookie is set and OAuth sessions appear in listDeviceSessions. Fixes #8060. - **Bug Fixes** - Detect and handle 3xx redirect Response values with isRedirectResponse in to-auth-endpoints. - Run after hooks on redirects and merge their headers (including Set-Cookie) into the final response. - Multi-session plugin now sets the _multi-* cookie during OAuth flows; email/password flows unchanged. <sup>Written for commit 4a9bd1fd62856fb7369a000670376cce1719f655. 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-04-15 22:28:29 -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#24618