[PR #5087] [CLOSED] feat(magic-link): add handler function support for disableSignUp #5765

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5087
Author: @GoPro16
Created: 10/4/2025
Status: Closed

Base: canaryHead: feat/magic-link-new-user


📝 Commits (2)

  • 67c5337 feat(magic-link): add handler function support for disableSignUp
  • 6d2a42e fix: evaluate disableSignUp function in verification flow

📊 Changes

2 files changed (+279 additions, -8 deletions)

View changed files

📝 packages/better-auth/src/plugins/magic-link/index.ts (+35 -8)
📝 packages/better-auth/src/plugins/magic-link/magic-link.test.ts (+244 -0)

📄 Description

Summary

Allow disableSignUp to accept a function that receives the request context and returns a boolean. This enables conditional signup blocking based on request headers, paths, or other contextual information.

Changes

  • Updated disableSignUp option to accept boolean | ((request: Request | undefined) => boolean | Promise<boolean>)
  • Added support for both synchronous and asynchronous handler functions
  • Properly handles cases where the request may be undefined
  • Only queries the database when signup is actually disabled (efficient)

Example Usage

magicLink({
  sendMagicLink: async ({ email, url, token }) => {
    await sendEmail(email, url);
  },
  disableSignUp: (request) => {
    if (!request) return false;
    const referer = request.headers.get("referer");
    return referer?.includes("/admin") ?? false;
  }
})

Or with async logic:

magicLink({
  disableSignUp: async (request) => {
    const isMaintenanceMode = await checkMaintenanceMode();
    return isMaintenanceMode;
  }
})

Tests

  • All existing tests pass
  • Added comprehensive tests for function handler behavior
  • Tests cover sync/async functions, request context access, and edge cases
  • TypeScript compilation successful

Summary by cubic

Adds handler function support for magicLink.disableSignUp, enabling conditional blocking of new signups based on the request context. Existing users can still sign in with magic links.

  • New Features
    • disableSignUp accepts a boolean or a function (sync/async) that receives Request | undefined.
    • Safely handles cases where the request is undefined.
    • Only checks the database when signup is disabled, and blocks only if the user doesn’t exist.
    • Keeps existing users unaffected even when disableSignUp returns true.

🔄 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/5087 **Author:** [@GoPro16](https://github.com/GoPro16) **Created:** 10/4/2025 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `feat/magic-link-new-user` --- ### 📝 Commits (2) - [`67c5337`](https://github.com/better-auth/better-auth/commit/67c5337046e35276ef4461ae123b6dc8c5d1274f) feat(magic-link): add handler function support for disableSignUp - [`6d2a42e`](https://github.com/better-auth/better-auth/commit/6d2a42e3433ca6692f00812d9ecc636c8c553cc9) fix: evaluate disableSignUp function in verification flow ### 📊 Changes **2 files changed** (+279 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/plugins/magic-link/index.ts` (+35 -8) 📝 `packages/better-auth/src/plugins/magic-link/magic-link.test.ts` (+244 -0) </details> ### 📄 Description ## Summary Allow `disableSignUp` to accept a function that receives the request context and returns a boolean. This enables conditional signup blocking based on request headers, paths, or other contextual information. ## Changes - Updated `disableSignUp` option to accept `boolean | ((request: Request | undefined) => boolean | Promise<boolean>)` - Added support for both synchronous and asynchronous handler functions - Properly handles cases where the request may be undefined - Only queries the database when signup is actually disabled (efficient) ## Example Usage ```typescript magicLink({ sendMagicLink: async ({ email, url, token }) => { await sendEmail(email, url); }, disableSignUp: (request) => { if (!request) return false; const referer = request.headers.get("referer"); return referer?.includes("/admin") ?? false; } }) ``` Or with async logic: ```typescript magicLink({ disableSignUp: async (request) => { const isMaintenanceMode = await checkMaintenanceMode(); return isMaintenanceMode; } }) ``` ## Tests - ✅ All existing tests pass - ✅ Added comprehensive tests for function handler behavior - ✅ Tests cover sync/async functions, request context access, and edge cases - ✅ TypeScript compilation successful <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds handler function support for magicLink.disableSignUp, enabling conditional blocking of new signups based on the request context. Existing users can still sign in with magic links. - **New Features** - disableSignUp accepts a boolean or a function (sync/async) that receives Request | undefined. - Safely handles cases where the request is undefined. - Only checks the database when signup is disabled, and blocks only if the user doesn’t exist. - Keeps existing users unaffected even when disableSignUp returns true. <!-- 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:34:53 -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#5765