Setup
1
Install
2
Add the plugin
lib/kavach.ts
3
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.