Installation
Add the server plugin
Import and configure
magicLink in your auth config. You must provide a sendMagicLink function that delivers the link to the user:auth.ts
Usage
Send a magic link
CallsignIn.magicLink on the client with the user’s email address:
sign-in.ts
If the email address is not registered and
disableSignUp is not true, the user is automatically signed up on first use.Verification flow
When the user clicks the link:- Better Auth validates the token.
- If valid, the user is authenticated and redirected to
callbackURL. - If invalid or expired, they are redirected to
callbackURL?error=...(orerrorCallbackURLif provided).
Manual verification
If you need to verify a token yourself (e.g. you built your own link format), call themagicLink.verify endpoint:
verify.ts
Server-side sendMagicLink callback
The function receives:
| Parameter | Description |
|---|---|
email | The user’s email address. |
url | The full magic link URL to send. Contains the token. |
token | The raw token, if you want to build a custom URL. |
metadata | Any extra data passed via signIn.magicLink. |
ctx context object is passed as the second argument.
auth.ts
Configuration options
| Option | Type | Default | Description |
|---|---|---|---|
sendMagicLink | function | — | Required. Called to send the magic link email. |
expiresIn | number | 300 | Token lifetime in seconds (5 minutes). |
allowedAttempts | number | 1 | Number of times a token can be used before it is invalidated. Set to Infinity for unlimited. |
disableSignUp | boolean | false | Prevent new user registration via magic link. |
generateToken | function | — | Custom token generator. Receives email, returns a string. Must return a cryptographically secure value. |
storeToken | string | "plain" | How to store the token: "plain", "hashed", or a custom hasher object. |
Token expiry example
auth.ts