Magic-link sign-in fails when the mail provider scans links (token spent before the user clicks)
What Happened?
Magic-link sign-in fails for mailboxes whose provider scans links in incoming mail (seen with Zoho Mail; Outlook/Microsoft Defender Safe Links, Mimecast, Proofpoint and similar behave the same way). The scanner opens the link before the user does. Because the email callback signs in on a plain GET and spends the one-time VerificationToken, the scanner gets the session and the user's own click lands on "Unable to sign in — The sign in link is no longer valid." Every resend fails the same way, so affected users cannot sign in at all.
Steps To Reproduce
- Self-host OpenReply with Resend (or
EMAIL_SERVERSMTP) and add a mailbox on a link-scanning provider (e.g. Zoho Mail) toALLOWED_EMAILS. - Request a sign-in link from
/login. - Open the link from the inbox.
Expected Behavior
Opening the link signs the user in.
Actual Behavior
Auth.js logs [auth][error] Verification and shows "The sign in link is no longer valid". The database shows the token was already consumed and a Session row created a few seconds after the email was sent, well before the user's click. Timeline from one deployment (UTC):
| Session created (token spent) | User's click (rejected) | |
|---|---|---|
| 1 | 18:50:56 | 18:52:16 |
| 2 | 18:52:46 | 18:53:04 |
| 3 | 18:53:51 | 18:54:16 |
Besides blocking sign-in, this leaves valid sessions in the scanner's hands.
Environment
- OS: Ubuntu 24.04 (aarch64), single-VM Docker Compose deployment (
compose.production.yml) - Node version: 20 (
node:20-slimimage) - Database: PostgreSQL 16
- Redis: 7
- Browser: any (the failure happens before the user's browser is involved)
- Auth:
next-auth5.0.0-beta.31, Resend provider
Logs Or Screenshots
[auth][error] Verification: Read more at https://errors.authjs.dev#verification
Suggested Fix
Make a plain visit to the magic link harmless and require an explicit click to spend the token. Scanners fetch pages but do not submit forms.
- In
proxy.ts, redirectGET /api/auth/callback/(resend|nodemailer)?token=…to a confirmation page unless the request carriesconfirm=1. This covers both email transports and needs no change to how emails are sent:
// Opening a magic link signs in and spends its one-time token, so mail
// scanners that prefetch links used the token before the user could. Plain
// visits go to a confirmation page instead; only its form submission, marked
// with `confirm=1`, reaches Auth.js.
const EMAIL_CALLBACK = /^\/api\/auth\/callback\/(resend|nodemailer)$/;
export function proxy(request: NextRequest) {
const pathname = request.nextUrl.pathname;
const { searchParams } = request.nextUrl;
const emailCallback = pathname.match(EMAIL_CALLBACK);
if (
emailCallback &&
searchParams.has("token") &&
searchParams.get("confirm") !== "1"
) {
const confirmUrl = new URL("/verify-request/confirm", request.url);
confirmUrl.search = searchParams.toString();
confirmUrl.searchParams.set("provider", emailCallback[1]);
return NextResponse.redirect(confirmUrl);
}
// ...existing logic
}
// and add "/api/auth/callback/:provider" to config.matcher
-
Add
app/verify-request/confirm/page.tsx: a "Confirm sign in" page (markednoindex) whose Sign in button submits aGETform to/api/auth/callback/<provider>with hiddentoken,email,callbackUrlandconfirm=1fields. Auth.js ignores the extraconfirmparameter. The provider is checked against an allow-list (resend,nodemailer) before it is used in the form action. -
Add translations for the new strings in
lib/i18n/zh-TW.json, plus proxy tests covering: plain visit → redirect with params preserved;confirm=1→ passes through; non-email callbacks untouched.
I have this running in production, with tests, type check and lint passing, and am happy to open a PR.