[PR #5089] feat(magic-link): add handler function support for disableSignUp #5767

Open
opened 2026-03-13 12:35:01 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

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

Base: canaryHead: feat/magic-link-handler-function


📝 Commits (5)

  • 67c5337 feat(magic-link): add handler function support for disableSignUp
  • 6d2a42e fix: evaluate disableSignUp function in verification flow
  • 5577bf8 docs(magic-link): document disableSignUp function handler support
  • 9a48b4c Update packages/better-auth/src/plugins/magic-link/index.ts
  • 5a48c81 refactor(magic-link): pass ctx instead of request to disableSignUp

📊 Changes

3 files changed (+316 additions, -17 deletions)

View changed files

📝 docs/content/docs/plugins/magic-link.mdx (+43 -9)
📝 packages/better-auth/src/plugins/magic-link/index.ts (+28 -8)
📝 packages/better-auth/src/plugins/magic-link/magic-link.test.ts (+245 -0)

📄 Description

Summary

Allow disableSignUp to accept a function that receives the endpoint context and returns a boolean. This enables conditional signup blocking based on request data, application state, or external conditions.

Changes

  • Updated disableSignUp option to accept boolean | ((ctx: GenericEndpointContext) => boolean | Promise<boolean>)
  • Added support for both synchronous and asynchronous handler functions
  • Function receives full endpoint context with access to body, context, and request
  • Function is evaluated in both the send and verify endpoints
  • Only queries the database when signup is actually disabled (efficient)

Example Usage

magicLink({
  sendMagicLink: async ({ email, url, token }) => {
    await sendEmail(email, url);
  },
  disableSignUp: (ctx) => {
    // Only allow signups from specific email domains
    const email = ctx.body.email;
    const allowedDomains = ["company.com", "partner.com"];
    const domain = email.split("@")[1];
    return !allowedDomains.includes(domain);
  }
})

Or with async logic:

magicLink({
  disableSignUp: async (ctx) => {
    // Check feature flag or application state
    const signupsEnabled = await checkFeatureFlag("magic-link-signups");
    return !signupsEnabled;
  }
})

Tests

  • All existing tests pass
  • Added comprehensive tests for function handler behavior
  • Tests cover sync/async functions, context access, and edge cases
  • Test verifies function is evaluated during verification flow
  • TypeScript compilation successful with proper types

🔄 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/5089 **Author:** [@GoPro16](https://github.com/GoPro16) **Created:** 10/4/2025 **Status:** 🔄 Open **Base:** `canary` ← **Head:** `feat/magic-link-handler-function` --- ### 📝 Commits (5) - [`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 - [`5577bf8`](https://github.com/better-auth/better-auth/commit/5577bf89e848227c1243a69bb486200711c504c4) docs(magic-link): document disableSignUp function handler support - [`9a48b4c`](https://github.com/better-auth/better-auth/commit/9a48b4cb9a038640240b8c4325400c4fc3c412ca) Update packages/better-auth/src/plugins/magic-link/index.ts - [`5a48c81`](https://github.com/better-auth/better-auth/commit/5a48c8195a4c8dc81db9ae095a29eb828ec588e9) refactor(magic-link): pass ctx instead of request to disableSignUp ### 📊 Changes **3 files changed** (+316 additions, -17 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/plugins/magic-link.mdx` (+43 -9) 📝 `packages/better-auth/src/plugins/magic-link/index.ts` (+28 -8) 📝 `packages/better-auth/src/plugins/magic-link/magic-link.test.ts` (+245 -0) </details> ### 📄 Description ## Summary Allow `disableSignUp` to accept a function that receives the endpoint context and returns a boolean. This enables conditional signup blocking based on request data, application state, or external conditions. ## Changes - Updated `disableSignUp` option to accept `boolean | ((ctx: GenericEndpointContext) => boolean | Promise<boolean>)` - Added support for both synchronous and asynchronous handler functions - Function receives full endpoint context with access to `body`, `context`, and `request` - Function is evaluated in both the send and verify endpoints - Only queries the database when signup is actually disabled (efficient) ## Example Usage ```typescript magicLink({ sendMagicLink: async ({ email, url, token }) => { await sendEmail(email, url); }, disableSignUp: (ctx) => { // Only allow signups from specific email domains const email = ctx.body.email; const allowedDomains = ["company.com", "partner.com"]; const domain = email.split("@")[1]; return !allowedDomains.includes(domain); } }) ``` Or with async logic: ```typescript magicLink({ disableSignUp: async (ctx) => { // Check feature flag or application state const signupsEnabled = await checkFeatureFlag("magic-link-signups"); return !signupsEnabled; } }) ``` ## Tests - ✅ All existing tests pass - ✅ Added comprehensive tests for function handler behavior - ✅ Tests cover sync/async functions, context access, and edge cases - ✅ Test verifies function is evaluated during verification flow - ✅ TypeScript compilation successful with proper types --- <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:35:01 -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#5767