A magic link email signs someone in when they click a link sent to their inbox. No password to remember, reset or leak. The link contains a random token that works once and expires within minutes. The parts that need care are the token rules, link scanners that click before your user does, and delivery speed, because nobody waits two minutes to log in.
How magic link login works
- The user types their email address and presses “Email me a link”.
- Your app creates a random token (32 bytes from a secure source), stores its hash with a short expiry, and emails a link containing the token.
- The user clicks the link. Your app hashes the token, finds the record, checks expiry and that it is unused, marks it used, and starts a session.
- As with resets, show the same message whether or not the address has an account, unless you also create accounts on first login.
The link scanner problem
Many corporate email security gateways open the URLs in incoming email to check them for malware before the recipient sees the message. If a plain GET to your link signs the user in and burns the token, the scanner uses it and your user sees “link expired”.
The fix: the link opens a page that says “Sign in to Acme as sam@example.com” with a button. The button sends a POST that consumes the token. Scanners load pages; they do not usually press buttons. You can also allow a token to be opened a few times within its window, as long as only one session is created.
Best practices
- Bind to the browser when you can. Store a random value in a cookie when the link is requested and check it on click. If the cookie is missing (another device), ask the user to confirm or enter the code instead.
- Add a code to the same email. A 6-digit code lets people on a different device finish signing in.
- Expire fast. 10 to 15 minutes is typical.
- Clear subject. “Your Acme sign-in link” is searchable and trusted.
- Do not rewrite the URL. Click tracking and link shorteners add a hop on another domain, which looks like phishing in a login email.
- Rate limit. Per address and per IP, so nobody can flood an inbox.
Magic link email template
From: Acme <login@acme.com>
Subject: Your Acme sign-in link
Sign in to Acme as sam@example.com:
https://app.acme.com/auth/magic?token=Zt8...q1
Or enter this code: 318 554
The link and code work once, for 15 minutes.
If you did not try to sign in, you can ignore this email.
Sending a magic link with Mailcandor
The send is one call to the Mailcandor email API. This Express route issues the token, sets the browser-binding cookie and sends the email:
import express from 'express';
import crypto from 'node:crypto';
const app = express();
app.use(express.urlencoded({ extended: false }));
const sha = (v) => crypto.createHash('sha256').update(v).digest('hex');
app.post('/auth/magic', async (req, res) => {
const email = String(req.body.email).trim().toLowerCase();
const token = crypto.randomBytes(32).toString('base64url');
const code = String(crypto.randomInt(0, 1_000_000)).padStart(6, '0');
const browser = crypto.randomBytes(16).toString('hex');
await saveLoginToken({
email, tokenHash: sha(token), codeHash: sha(code), browserHash: sha(browser),
expiresAt: Date.now() + 15 * 60 * 1000,
});
const link = `https://app.acme.com/auth/magic?token=${token}`;
const r = await fetch('https://api.mailcandor.com/v1/emails', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.MAILCANDOR_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
from: 'Acme <login@acme.com>',
to: email,
subject: 'Your Acme sign-in link',
html: `<p>Sign in to Acme as ${email}:</p>
<p><a href="${link}">Sign in</a></p>
<p>Or enter this code: <b>${code.slice(0, 3)} ${code.slice(3)}</b></p>
<p>The link and code work once, for 15 minutes.</p>`,
text: `Sign in to Acme: ${link}\nOr enter this code: ${code}\nThe link and code work once, for 15 minutes.`,
tags: [{ name: 'flow', value: 'magic_link' }],
}),
});
if (!r.ok) console.error('magic link send failed', (await r.json()).error);
res.cookie('login_browser', browser, { httpOnly: true, secure: true, sameSite: 'lax', maxAge: 15 * 60 * 1000 });
res.send('If that address can sign in, a link is on its way.');
});
app.listen(3000);
saveLoginToken is your own database call. The GET handler for /auth/magic should only render a confirm page; a POST from that page checks the token hash, the expiry, the login_browser cookie, and then starts the session.
The email is accepted in milliseconds and the response includes an id; delivery, bounce and failure events arrive by webhook tagged magic_link, so you can see if logins are failing for one mailbox provider. A sending-only key locked to your login domain keeps a leaked key from sending anything else.
Delivery tips specific to login email
Login email is judged harshly because phishing looks similar. Send from your own verified domain with SPF, DKIM and DMARC passing, keep the From address stable, and make the link domain match the sender domain. If corporate users report missing links, check whether their gateway is quarantining them; our Outlook spam guide covers Microsoft-specific causes. Keep marketing mail on a separate subdomain so its complaints never affect logins.
Common pitfalls
- GET consumes the token. Scanners burn it. Confirm with a POST.
- Reusable links. A link that signs in forever is a password sitting in an inbox.
- Raw tokens in the database. Store hashes only.
- Revealing account existence. Same response for every address.
- Links built from the Host header. Use a configured base URL so attackers cannot redirect the link.
- Slow sending. Queue only for retries. Send the first attempt during the request or right after it.