Prompt
How do I implement magic link login for customers?
Latest observation
Nov 1, 2026
OpenAI APIWeb search: off
A good magic-link login flow for customers usually looks like this:
1) User enters email
Customer submits their email address on a “Log in” form.
2) Create a one-time login token
On the backend:
- generate a cryptographically random token
- store only a hashed version of it in your database
- attach metadata:
- user/customer ID or email
- expiration time (e.g. 10–20 minutes)
- used/revoked flag
- optional: IP/user-agent for auditing
3) Email the link
Send an email containing a URL like:
https://yourapp.com/magic-login?token=RANDOM_TOKEN
Or better, use a path like:
https://yourapp.com/auth/magic/confirm?token=RANDOM_TOKEN
4) Verify token when clicked
When the user opens the link:
- hash the received token
- look it up in your DB
- ensure it exists, is unexpired, and unused
- mark it as used immediately
- create a session or issue a JWT
- redirect the user into the app
5) Set a secure session
Prefer a secure, httpOnly cookie session over putting auth info in the URL or localStorage.
Recommended implementation details
Token generation
Use a secure random value, at least 32 bytes.
Example:
- raw token:
base64url(randomBytes(32)) - store:
sha256(token)
Why hash it? If your DB leaks, attackers can’t use the login links directly.
Expiration
Keep the token short-lived:
- 10 minutes is common
- 15 minutes is also fine
Single use
Once a token is used, invalidate it immediately.
Rate limiting
Protect the request endpoint:
- limit by IP
- limit by email address
- prevent enumeration by always returning a generic response like:
- “If an account exists, we’ve sent a login link.”
Session creation
After verification:
- create server-side session
- set cookie:
HttpOnlySecureSameSite=LaxorStrictdepending on your flow
Minimal flow example
Request link
POST /auth/magic/request
Body:
{ "email": "customer@example.com" }
Backend:
- find user by email
- generate token
- store hashed token + expiry
- email link
- return generic success
Confirm link
GET /auth/magic/confirm?token=...
Backend:
- hash token
- verify record
- check expiry/unused
- create session
- redirect to dashboard
Security tips
- Don’t reveal whether an email exists in the system
- Use HTTPS only
- Make tokens long and random
- Invalidate on use
- Consider tying token use to a rough device/IP fingerprint if your UX allows it
- Log events for abuse detection
- Add “resend link” cooldowns
Common pitfalls
- Storing raw tokens in DB: avoid this
- Long-lived magic links: risky
- No rate limit: easy to abuse
- Putting auth data in the URL permanently: clear it after login
- Allowing token reuse: dangerous
Optional enhancements
- Show a confirmation page after clicking the link before logging in
- Support “login from same browser” checks
- Offer fallback password login
- Add WebAuthn/passkeys later for stronger auth
If you want, I can give you:
- a Node/Express implementation,
- a Django implementation, or
- a database schema + email template for magic links.