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

Open
opened 2026-04-13 09:32:21 -05:00 by GiteaMirror · 0 comments
Owner

Original Pull Request: https://github.com/better-auth/better-auth/pull/5969

State: open
Merged: No


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.

**Original Pull Request:** https://github.com/better-auth/better-auth/pull/5969 **State:** open **Merged:** No --- ### 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. -->
GiteaMirror added the pull-request label 2026-04-13 09:32:21 -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#14599