Prompt
How do I send magic link login emails?
Latest observation
Nov 17, 2026
OpenAI APIWeb search: off
To send magic link login emails, you generally need four pieces:
- A backend endpoint that creates a one-time login token
- A database or cache to store that token temporarily
- An email service to send the link
- A verification endpoint that consumes the token and logs the user in
Typical flow
- User enters their email address.
- Your server generates a secure, random token.
- Store a hashed version of the token with:
- the user email or user ID
- expiration time
- one-time-use flag
- Email a link like:
https://yourapp.com/magic-login?token=abc123
- When the user clicks it:
- your server checks the token
- verifies it’s valid and unexpired
- marks it as used
- creates a session / JWT / auth cookie
Example implementation steps
1) Generate a secure token
Use a cryptographically secure random value.
import crypto from "crypto";
const token = crypto.randomBytes(32).toString("hex");
2) Store token securely
Prefer storing a hash of the token instead of the token itself.
const tokenHash = crypto.createHash("sha256").update(token).digest("hex");
Store:
emailtokenHashexpiresAtusedAt(null until consumed)
3) Send the email
Use a transactional email provider such as:
- SendGrid
- Postmark
- Mailgun
- Amazon SES
- Resend
Example email body:
Click to sign in:
https://yourapp.com/auth/magic?token=YOUR_TOKEN
This link expires in 15 minutes.
If you didn’t request this, ignore this email.
4) Verify token on click
When the link is visited:
- hash the provided token
- look it up in your DB
- ensure it exists, is not expired, and not used
- create a session
- invalidate token
Example backend logic outline
Request magic link
app.post("/auth/magic-link", async (req, res) => {
const { email } = req.body;
const token = crypto.randomBytes(32).toString("hex");
const tokenHash = crypto.createHash("sha256").update(token).digest("hex");
await db.magicLinks.insert({
email,
tokenHash,
expiresAt: new Date(Date.now() + 15 * 60 * 1000),
usedAt: null,
});
const link = `https://yourapp.com/auth/magic?token=${token}`;
await sendEmail(email, "Your login link", `Click here: ${link}`);
res.json({ ok: true });
});
Verify magic link
app.get("/auth/magic", async (req, res) => {
const { token } = req.query;
const tokenHash = crypto.createHash("sha256").update(token).digest("hex");
const record = await db.magicLinks.findOne({ tokenHash });
if (!record || record.usedAt || record.expiresAt < new Date()) {
return res.status(400).send("Invalid or expired link");
}
await db.magicLinks.update(record.id, { usedAt: new Date() });
// Create session / set cookie / issue JWT
req.session.userEmail = record.email;
res.redirect("/dashboard");
});
Security best practices
- Expire links quickly: 10–30 minutes is common
- Use one-time-use tokens
- Store hashed tokens, not raw tokens
- Rate limit requests by IP/email
- Avoid revealing whether an email exists if you care about account enumeration
- Send from a verified domain
- Protect against link forwarding if needed by binding to device/session signals (optional)
If you want the easiest path
Many auth providers already support magic links:
- Clerk
- Auth0
- Supabase Auth
- Firebase Auth
- Magic.link
- NextAuth (email provider)
If you tell me your stack (e.g. Node/Express, Next.js, Python/Django, Rails, etc.), I can give you a ready-to-use implementation.