Setup
Install
Add the plugin
lib/kavach.ts
Handle the callback route
Magic links redirect tobaseUrl + /auth/magic-link/verify?token=.... KavachOS handles this automatically. Set redirectTo to control where users land after sign-in:How it works
- User submits their email to
POST /auth/magic-link/send. - KavachOS generates a signed token and calls your
onSendLinkfunction with the email address and the full URL. - User clicks the link in their inbox.
- KavachOS validates the token, creates or retrieves the user, sets a session cookie, and redirects.
Endpoints
Send link
POST /auth/magic-link/send
200 to prevent email enumeration. Attach a redirectTo in the body to override the default redirect for this request:
Verify token
GET /auth/magic-link/verify?token=<token>
KavachOS handles this automatically when the user clicks the link. On success, the user is redirected. On failure (expired or already-used token), a 400 is returned.
Rate limiting
Requests to/auth/magic-link/send are limited to 5 per minute per IP address. Requests that exceed this return 429 Too Many Requests. Build a cooldown timer into your UI so users know when they can retry.
Options
| Option | Type | Default | Description |
|---|---|---|---|
onSendLink | (email: string, url: string) => Promise<void> | required | Called with the recipient email and the full magic link URL |
tokenTtl | number | 900 | Token lifetime in seconds (default: 15 minutes) |
redirectTo | string | / | Where to redirect after successful sign-in |
createUserIfNotFound | boolean | true | Auto-create accounts for new emails |