[PR #3417] feat(magic-link): allow validated additional data for the send email function #13056

Open
opened 2026-04-13 08:43:26 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/3417
Author: @Gittified
Created: 7/17/2025
Status: 🔄 Open

Base: mainHead: feat/magic-link-additional-data


📝 Commits (10+)

  • 6cf6f44 feat(plugins/magic-link): add additionalData to body and function definition
  • 7efa435 docs(plugins/magic-link): add documentation for additional data
  • e6da874 refactor(plugins/magic-link): use Zod schema for type validation
  • 6167606 docs(plugins/magic-link): fixed zod import in example
  • 195ce57 refactor(plugins/magic-link): forgot to lint the file
  • 8c67f19 ref(plugins/magic-link): remove double type assertion
  • 7de2f0c chore(magic-link): format code once more
  • 8433ca2 chore(magic-link): clean up & add meta to endpoint body
  • 84fabdb ref(plugins/magic-link): refine type safety with Json schema
  • f7a4c18 docs(plugins/magic-link): refine documentation for plugin

📊 Changes

2 files changed (+99 additions, -5 deletions)

View changed files

📝 docs/content/docs/plugins/magic-link.mdx (+52 -1)
📝 packages/better-auth/src/plugins/magic-link/index.ts (+47 -4)

📄 Description

Magic Link

Motivation

When working on my own application, I came across the issue that I can currently not provide any "custom" data from the client to the function that eventually sends the magic link email. This had me brainstorming, but eventually I figured that I might as well contribute a solution.

My specific use-case is that when someone attempts to log in, I want to show the country that the request came from. Since I use Next.js server actions to call most of my auth functions, the provided Request object is undefined because no actual HTTP request takes place. This is frustrating because I am able to add headers and a body on the server API but not actually use them there that way. At first, I considered simply changing ctx.request to ctx and just providing the whole context, but that solution didn't feel right, is difficult to make type-safe and would be a "breaking change".

Implementation

  • Added the additionalDataSchema property to the MagicLinkOptions allowing a Zod scheme to be configured, defaults to Record<string, any>.
  • Added the Zod scheme to the request body under the additionalData property.
  • Added the additionalData property to the function signature of sendMagicLink()
  • Works on both the server & client API.

Testing

I do not see the need for new tests here.

  • I've noticed that my code is impacted by #3399 which changes the way ZodObject works. Fixed

Checklist

  • Updated documentation
  • Types work well
  • Code has been tested
  • Backward compatibility maintained

Summary by cubic

Added support for passing custom additional data to the magic link email function, with optional Zod schema validation for type safety.

  • New Features
    • Accepts an additionalData field in the magic link request body.
    • Allows defining an additionalDataSchema for validation and type inference.
    • Passes additionalData to the sendMagicLink function on both server and client.

🔄 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/3417 **Author:** [@Gittified](https://github.com/Gittified) **Created:** 7/17/2025 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/magic-link-additional-data` --- ### 📝 Commits (10+) - [`6cf6f44`](https://github.com/better-auth/better-auth/commit/6cf6f44fa603094c9188d28100427779f117cc58) feat(plugins/magic-link): add additionalData to body and function definition - [`7efa435`](https://github.com/better-auth/better-auth/commit/7efa4354faa0007fa50279f9e951c155c2ad7081) docs(plugins/magic-link): add documentation for additional data - [`e6da874`](https://github.com/better-auth/better-auth/commit/e6da87401452d66fdcb1bd6aeb244a28390ddb7f) refactor(plugins/magic-link): use Zod schema for type validation - [`6167606`](https://github.com/better-auth/better-auth/commit/616760668099683fdbe7508eaae4a4f1353b8ed7) docs(plugins/magic-link): fixed zod import in example - [`195ce57`](https://github.com/better-auth/better-auth/commit/195ce57bbe4ab80df907e7bcf2d7910ea2ef72bc) refactor(plugins/magic-link): forgot to lint the file - [`8c67f19`](https://github.com/better-auth/better-auth/commit/8c67f19820146e410191f551decf3af644ada168) ref(plugins/magic-link): remove double type assertion - [`7de2f0c`](https://github.com/better-auth/better-auth/commit/7de2f0c4d0a3ebe1f0c7e2b2b527735c696e102c) chore(magic-link): format code once more - [`8433ca2`](https://github.com/better-auth/better-auth/commit/8433ca202446cbfedd08b378d18c31819495d67b) chore(magic-link): clean up & add meta to endpoint body - [`84fabdb`](https://github.com/better-auth/better-auth/commit/84fabdba3d3087ff3e0756ad9ac08d3948e969c2) ref(plugins/magic-link): refine type safety with Json schema - [`f7a4c18`](https://github.com/better-auth/better-auth/commit/f7a4c18f6f733800cfa0621ac1c96470e09d1c3d) docs(plugins/magic-link): refine documentation for plugin ### 📊 Changes **2 files changed** (+99 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/plugins/magic-link.mdx` (+52 -1) 📝 `packages/better-auth/src/plugins/magic-link/index.ts` (+47 -4) </details> ### 📄 Description # Magic Link ## Motivation When working on my own application, I came across the issue that I can currently not provide any "custom" data from the client to the function that eventually sends the magic link email. This had me brainstorming, but eventually I figured that I might as well contribute a solution. My specific use-case is that when someone attempts to log in, I want to show the country that the request came from. Since I use Next.js server actions to call most of my auth functions, the provided `Request` object is `undefined` because no actual HTTP request takes place. This is frustrating because I am able to add headers and a body on the server API but not actually use them there that way. At first, I considered simply changing `ctx.request` to `ctx` and just providing the whole context, but that solution didn't feel right, is difficult to make type-safe and would be a "breaking change". ## Implementation - Added the `additionalDataSchema` property to the `MagicLinkOptions` allowing a Zod scheme to be configured, defaults to `Record<string, any>`. - Added the Zod scheme to the request body under the `additionalData` property. - Added the `additionalData` property to the function signature of `sendMagicLink()` - Works on both the server & client API. ## Testing I do not see the need for new tests here. ## Related - ~~I've noticed that my code is impacted by #3399 which changes the way `ZodObject` works.~~ Fixed ## Checklist - Updated documentation - Types work well - Code has been tested - Backward compatibility maintained <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Added support for passing custom additional data to the magic link email function, with optional Zod schema validation for type safety. - **New Features** - Accepts an `additionalData` field in the magic link request body. - Allows defining an `additionalDataSchema` for validation and type inference. - Passes `additionalData` to the `sendMagicLink` function on both server and client. <!-- 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-13 08:43:26 -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#13056