How to test magic link login
Magic links remove passwords, but they move all of your login security into one email. That email deserves real tests.
What to check
- It arrives, quickly, at the address that asked for it.
- The link points to the right domain for this environment.
- It signs the user in and lands on the right page.
- It works once. Opening it again shouldn't sign in again.
- It expires. An old link is refused with a clear message and a way to get a new one.
- A new link replaces the old one, if that's how you designed it.
Playwright example
Using the helper from email verification in Playwright (createInbox, waitForEmail, extractLink):
import { test, expect } from '@playwright/test';
import { createInbox, waitForEmail, extractLink } from './helpers/inbox';
test('magic link signs in once', async ({ page, browser }) => {
test.setTimeout(90_000);
const inbox = await createInbox();
await page.goto('/login');
await page.getByLabel('Email').fill(inbox.address);
await page.getByRole('button', { name: 'Email me a link' }).click();
const email = await waitForEmail(inbox.local, { subject: 'Sign in' });
const link = extractLink(email, /\/auth\/magic/);
expect(new URL(link).host).toBe(new URL(page.url()).host);
await page.goto(link);
await expect(page.getByText('Dashboard')).toBeVisible();
// a second use, in a fresh browser, must not work
const other = await (await browser.newContext()).newPage();
await other.goto(link);
await expect(other.getByText(/expired|already used/i)).toBeVisible();
});Testing expiry
Waiting 15 minutes in a test is slow. Better options: a short expiry in your test environment, or a test-only endpoint that ages a token. Keep one slow nightly test with the real expiry if you can.
Watch out for link scanners
Some company mail systems open links to scan them, which can use up a one-time link before the user clicks. A common fix is a confirmation page with a "Sign in" button instead of signing in on the first GET.
Want an AI agent to run these checks? See magic link testing with an AI agent.
Related guides
- How to test email OTP and 2FA codes
- How to test emails in GitHub Actions
- How to test transactional email templates: links, content and spam score
- Disposable email API for developers
- Email testing API overview
Try it now: the free endpoints above need no signup and are documented on the temporary email API page. For private inboxes, webhooks and CI at scale, the QuickInbox Business API has a free plan with 5,000 emails a month.