Passwordless sign-in
Overview and step-by-step setup for magic links and email OTPs.
Magic links and email OTPs are additional first factors: passwords keep working exactly as before. Both prove control of an email address, mark it verified, and create a Session. A User created this way has no credential Account until they set a password.
Choose a factor:
- Magic-link sign-in — email a link; clicking it signs the User in.
- Email OTP sign-in — email a six-digit code; submit the code to sign in.
- Passwordless verification — proofs, expiry, replay, callbacks and error handling.
Step-by-step setup
1. Start from a secure Config
signet.New needs a Store, Mailer, RateLimiter and 32-byte CSRF key. Set AppURL to your
origin; Signet prepends PathPrefix when building the link in the email. With AppURL = "https://app.example.com" and PathPrefix = "/auth", the link points to
https://app.example.com/auth/verify-magic-link?....
cfg := signet.DefaultConfig()
cfg.Store = store // sqlite.New(db) or postgres.New(pool)
cfg.Mailer = mailer // console.New(os.Stdout) in development
cfg.RateLimiter = limiter
cfg.CSRFKey = csrfKey // exactly 32 random bytes
cfg.AppURL = "https://app.example.com"
cfg.PathPrefix = "/auth"2. Add the Passwordless config block
PasswordlessConfig is optional: leaving it nil gives the same defaults. Both proof lifetimes
default to five minutes. AllowSignUp is a *bool; nil or true creates a new User when the
email is unknown. An explicit false makes sends for unknown emails silent successes with no
mail.
allowSignUps := true
cfg.Passwordless = &signet.PasswordlessConfig{
MagicLinkTTL: 5 * time.Minute,
OTPTTL: 5 * time.Minute,
AllowSignUp: &allowSignUps,
}Negative TTLs are rejected by New with ErrInvalidPasswordlessConfig.
3. Allowlist your callback URLs
A magic link can carry a callback_url. It must match an entry in
Passwordless.AllowedRedirects, then OAuth.AllowedRedirects, then AppURL exactly. A
same-origin path that is not listed is rejected; there is no wildcard matching.
cfg.Passwordless.AllowedRedirects = []string{
"https://app.example.com/welcome",
}If you omit callback_url, verifying the link returns session JSON instead of redirecting.
4. Configure send limits
RateLimitRule chooses a limiter and key prefix; it has no window. Supply a window-aware
limiter yourself and attach it to the published endpoint names. Recommended starting values:
5 requests/minute for send-magic-link, 3/minute for send-otp.
// windowLimiter is a minimal fixed-window RateLimiter. Use a shared store in production.
type windowLimiter struct {
mu sync.Mutex
hits map[string]int
window time.Duration
max int
}
func (l *windowLimiter) Allow(_ context.Context, key string) (bool, error) {
l.mu.Lock()
defer l.mu.Unlock()
l.hits[key]++
return l.hits[key] <= l.max, nil
}
func perMinute(max int) signet.RateLimiter {
return &windowLimiter{hits: make(map[string]int), window: time.Minute, max: max}
}
cfg.RateLimitRules = map[string]signet.RateLimitRule{
"send-magic-link": {Limiter: perMinute(5), KeyPrefix: "magic"},
"send-otp": {Limiter: perMinute(3), KeyPrefix: "otp"},
}The window field is illustrative — a real limiter should expire counts. The important part is
that you own the window; Signet only calls Allow(key).
Failed OTP guesses are throttled on verify-otp by email + client IP, without changing the
Store contract.
5. Build the Client and mount the router
auth, err := signet.New(cfg)
if err != nil {
log.Fatal(err)
}
mux := http.NewServeMux()
mux.Handle("/auth/", auth.Handler()) // the handler serves /auth/... itself; no StripPrefix6. Send and verify a proof
From Go code:
err := auth.SendMagicLink(ctx, signet.SendMagicLinkInput{
Email: "ada@example.com", CallbackURL: "https://app.example.com/welcome",
})
result, err := auth.VerifyMagicLink(ctx, signet.VerifyMagicLinkInput{Token: tokenFromEmail})
err = auth.SendEmailOTP(ctx, signet.SendEmailOTPInput{Email: "grace@example.com"})
result, err = auth.VerifyEmailOTP(ctx, signet.VerifyEmailOTPInput{
Email: "grace@example.com", Code: "123456",
})From a browser, use the four HTTP endpoints under your PathPrefix:
| Endpoint | Method | Request | Success |
|---|---|---|---|
send-magic-link | POST | {"email": "...", "callback_url": "..."} | 200 |
verify-magic-link | GET | ?token=...&callback_url=... | 302 redirect, or 200 session JSON |
send-otp | POST | {"email": "..."} | 200 |
verify-otp | POST | {"email": "...", "code": "123456"} | 200 session JSON |
All four are anonymous and CSRF-exempt. Verification sets the session and CSRF cookies.
What happens on success
- The proof is consumed atomically and cannot be used again.
- The email is marked
EmailVerified = true. An existing password and Sessions are untouched. - A new Session is created. The Go API returns its raw token in
SignInResult.SessionToken; the HTTP API sets the cookie and returns the token in the body. - If the email was unknown and auto-create is allowed, a new User is created with no credential Account.
Configuration reference
| Field | Default | Purpose |
|---|---|---|
MagicLinkTTL | 5 minutes | Lifetime of a magic-link proof. |
OTPTTL | 5 minutes | Lifetime of an email OTP. |
AllowSignUp | nil (true) | Auto-create a User for an unknown email. |
AllowedRedirects | empty | Exact-match callback allowlist before OAuth and AppURL fallbacks. |
Custom email templates are optional interfaces — MagicLinkTemplates and EmailOTPTemplates.
See Mail and the full complete example.