[PR #5969] Feat/5876 magic link mobile error support #31953

Open
opened 2026-04-17 22:49:38 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5969
Author: @Ridhim-RR
Created: 11/13/2025
Status: 🔄 Open

Base: mainHead: feat/5876-magic-link-mobile-error-support


📝 Commits (10+)

  • edf12f8 Store activeOrganizationSlug and activeOrganizationRole in Session by Default (#5706)
  • 2a9f4f7 Merge branch 'canary' into feat/add-slug-role-in-session
  • 720b5b7 chore: fix unsorted import and export statements
  • 02c4d16 bulk session update handled
  • 802c9ce test case issue
  • 6832140 chore: format-issue
  • 66fd63c fix: prisma snapshot
  • 480942c Merge branch 'canary' into feat/add-slug-role-in-session
  • 772278d feat(auth): add magic link error flow handling when no errorCallbackURL is provided (#5810)
  • 1f8237c fix(magic-link): narrow type before accessing token in test

📊 Changes

12 files changed (+986 additions, -42 deletions)

View changed files

📝 packages/better-auth/src/plugins/magic-link/index.ts (+61 -13)
📝 packages/better-auth/src/plugins/magic-link/magic-link.test.ts (+86 -2)
📝 packages/better-auth/src/plugins/organization/adapter.ts (+22 -3)
📝 packages/better-auth/src/plugins/organization/call.ts (+4 -0)
📝 packages/better-auth/src/plugins/organization/organization.test.ts (+413 -6)
📝 packages/better-auth/src/plugins/organization/organization.ts (+55 -0)
📝 packages/better-auth/src/plugins/organization/routes/crud-members.test.ts (+214 -1)
📝 packages/better-auth/src/plugins/organization/routes/crud-members.ts (+53 -5)
📝 packages/better-auth/src/plugins/organization/routes/crud-org.ts (+56 -2)
📝 packages/better-auth/src/plugins/organization/schema.ts (+8 -0)
📝 packages/better-auth/src/plugins/organization/types.ts (+2 -0)
📝 packages/cli/test/__snapshots__/schema-mysql-custom.prisma (+12 -10)

📄 Description

Closes #5876

This PR implements improved error handling for the magic link verification endpoint, specifically for mobile clients. Previously, when an invalid token was provided and no errorCallbackURL was set, the backend would default to redirecting to the root path with an error query parameter, which is not suitable for mobile apps. This change ensures that the verify function resolves with a JSON error object (e.g., { code: 'INVALID_TOKEN' }) when called from a mobile client or when no callback URL is provided, allowing proper client-side error handling.

Changes

  • Modified the magic link verification endpoint to return a JSON error object (e.g., { code: 'INVALID_TOKEN' }) when called from a mobile client or when no callback URL is provided.
  • Updated OpenAPI metadata to document both JSON and redirect response types.
  • Added a check to differentiate between requests from the browser and mobile apps using the x-better-auth-client: mobile header.
  • Ensured backward compatibility for web clients by maintaining redirect behavior when a callback URL is provided.
  • Added test cases specifically for requests from mobile apps.

Note: On the mobile client side, users need to send the header x-better-auth-client: mobile in the request.

Impact

  • Mobile clients can now handle errors directly without relying on redirects.
  • Web clients continue to receive redirects as before, ensuring no breaking changes for existing integrations.

Testing Strategy

  • Verified that mobile clients receive a JSON error object when an invalid or expired token is provided and no callback URL is set.
  • Confirmed that web clients still receive redirects when a callback URL is provided.
  • Tested token expiration and other error scenarios to ensure consistent behavior.

Summary by cubic

Improves magic link verification for mobile by returning JSON error codes instead of redirects, and stores active organization slug and role in the session for faster permission checks and consistent updates. Addresses #5876.

  • New Features

    • Magic link verify returns JSON errors (e.g., INVALID_TOKEN, EXPIRED_TOKEN) for mobile or when no callback URL; web keeps redirect behavior.
    • OpenAPI updated to document both JSON and redirect responses.
    • Session now tracks activeOrganizationSlug and activeOrganizationRole; kept in sync on setActive, role changes, slug updates, member removal, and org deletion.
    • Permission checks can use the session role for the active org to reduce database queries.
  • Migration

    • Mobile clients must send the header x-better-auth-client: mobile on verify requests.
    • If using a custom Session schema, add fields activeOrganizationSlug and activeOrganizationRole.

Written for commit 1f8237cdb7. Summary will update automatically 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/5969 **Author:** [@Ridhim-RR](https://github.com/Ridhim-RR) **Created:** 11/13/2025 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/5876-magic-link-mobile-error-support` --- ### 📝 Commits (10+) - [`edf12f8`](https://github.com/better-auth/better-auth/commit/edf12f8e0be4bcc15740d014489f2a76cc8596d1) Store activeOrganizationSlug and activeOrganizationRole in Session by Default (#5706) - [`2a9f4f7`](https://github.com/better-auth/better-auth/commit/2a9f4f7334c909cca260698b1f4bd345ea198872) Merge branch 'canary' into feat/add-slug-role-in-session - [`720b5b7`](https://github.com/better-auth/better-auth/commit/720b5b7f2c5c77be9ab3ba3bf4169801bdcf1a5a) chore: fix unsorted import and export statements - [`02c4d16`](https://github.com/better-auth/better-auth/commit/02c4d16bb6e0bad7dc34506d383da1fa377c7d18) bulk session update handled - [`802c9ce`](https://github.com/better-auth/better-auth/commit/802c9ce19f64ddaf15db27d098d86532610334bc) test case issue - [`6832140`](https://github.com/better-auth/better-auth/commit/6832140cd4b68e93a85895ad42197542beb454cd) chore: format-issue - [`66fd63c`](https://github.com/better-auth/better-auth/commit/66fd63ccc51eb71742067f22a687aebe2e225a91) fix: prisma snapshot - [`480942c`](https://github.com/better-auth/better-auth/commit/480942c6deaa6513231d1dcc0b8b8c9983fc474e) Merge branch 'canary' into feat/add-slug-role-in-session - [`772278d`](https://github.com/better-auth/better-auth/commit/772278db1d088d671b10db4558ee379d2520ebcd) feat(auth): add magic link error flow handling when no errorCallbackURL is provided (#5810) - [`1f8237c`](https://github.com/better-auth/better-auth/commit/1f8237cdb70cf0f97ff3c8f084c2c06ddcad27fd) fix(magic-link): narrow type before accessing token in test ### 📊 Changes **12 files changed** (+986 additions, -42 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/plugins/magic-link/index.ts` (+61 -13) 📝 `packages/better-auth/src/plugins/magic-link/magic-link.test.ts` (+86 -2) 📝 `packages/better-auth/src/plugins/organization/adapter.ts` (+22 -3) 📝 `packages/better-auth/src/plugins/organization/call.ts` (+4 -0) 📝 `packages/better-auth/src/plugins/organization/organization.test.ts` (+413 -6) 📝 `packages/better-auth/src/plugins/organization/organization.ts` (+55 -0) 📝 `packages/better-auth/src/plugins/organization/routes/crud-members.test.ts` (+214 -1) 📝 `packages/better-auth/src/plugins/organization/routes/crud-members.ts` (+53 -5) 📝 `packages/better-auth/src/plugins/organization/routes/crud-org.ts` (+56 -2) 📝 `packages/better-auth/src/plugins/organization/schema.ts` (+8 -0) 📝 `packages/better-auth/src/plugins/organization/types.ts` (+2 -0) 📝 `packages/cli/test/__snapshots__/schema-mysql-custom.prisma` (+12 -10) </details> ### 📄 Description ### Closes #5876 This PR implements improved error handling for the magic link verification endpoint, specifically for mobile clients. Previously, when an invalid token was provided and no errorCallbackURL was set, the backend would default to redirecting to the root path with an error query parameter, which is not suitable for mobile apps. This change ensures that the verify function resolves with a JSON error object (e.g., { code: 'INVALID_TOKEN' }) when called from a mobile client or when no callback URL is provided, allowing proper client-side error handling. ### Changes - Modified the magic link verification endpoint to return a JSON error object (e.g., { code: 'INVALID_TOKEN' }) when called from a mobile client or when no callback URL is provided. - Updated OpenAPI metadata to document both JSON and redirect response types. - Added a check to differentiate between requests from the browser and mobile apps using the x-better-auth-client: mobile header. - Ensured backward compatibility for web clients by maintaining redirect behavior when a callback URL is provided. - Added test cases specifically for requests from mobile apps. ### Note: On the mobile client side, users need to send the header x-better-auth-client: mobile in the request. ### Impact - Mobile clients can now handle errors directly without relying on redirects. - Web clients continue to receive redirects as before, ensuring no breaking changes for existing integrations. ### Testing Strategy - Verified that mobile clients receive a JSON error object when an invalid or expired token is provided and no callback URL is set. - Confirmed that web clients still receive redirects when a callback URL is provided. - Tested token expiration and other error scenarios to ensure consistent behavior. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Improves magic link verification for mobile by returning JSON error codes instead of redirects, and stores active organization slug and role in the session for faster permission checks and consistent updates. Addresses #5876. - **New Features** - Magic link verify returns JSON errors (e.g., INVALID_TOKEN, EXPIRED_TOKEN) for mobile or when no callback URL; web keeps redirect behavior. - OpenAPI updated to document both JSON and redirect responses. - Session now tracks activeOrganizationSlug and activeOrganizationRole; kept in sync on setActive, role changes, slug updates, member removal, and org deletion. - Permission checks can use the session role for the active org to reduce database queries. - **Migration** - Mobile clients must send the header x-better-auth-client: mobile on verify requests. - If using a custom Session schema, add fields activeOrganizationSlug and activeOrganizationRole. <sup>Written for commit 1f8237cdb70cf0f97ff3c8f084c2c06ddcad27fd. Summary will update automatically 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-17 22:49: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#31953