Test Auth0 passwordless email-code login with Playwright
Auth0 can sign users in with a one-time code sent by email, called passwordless login with the email connection. Your app redirects to Auth0's Universal Login page, the user enters an address, Auth0 emails a code, and the user types it in and is sent back to the app. A Playwright test can run this whole journey if it can read the email. This guide shows how, on a development tenant, with a unique AssertInbox address for each test. It does not describe Auth0's configuration in detail; for that, use the Auth0 documentation for your version of Universal Login and the passwordless email connection.
How it works
Mail sent to <namespace>.<tag>@assertinbox.com is stored for your key. GET /v1/wait long-polls for up to 25 seconds and returns the newest matching message as JSON, including id, subject, otp and magic_link, or 408 if nothing arrived. Messages are deleted after 24 hours. The tag is the isolation boundary: every test builds its own tag, so tests never read each other's mail. Auth0 sends the code to whatever address the test types in, so a fresh address per test means a fresh login per test.
Get an API key
Get one in a click at /start, or with the request below. One request creates a key and your private namespace. No email address or card is needed. The key is shown once, so store it in your secret manager or CI secrets.
curl -s -X POST https://assertinbox.com/v1/keys
# {"api_key":"ai_...","namespace":"k3x9a1bz","address_template":"k3x9a1bz.<tag>@assertinbox.com"}
Export ASSERTINBOX_KEY (the api_key) and ASSERTINBOX_NAMESPACE (the namespace). If the call returns 403 signups_closed, the beta is not open for new keys. Use inboxes only for services you own or control; see the terms. Limits and error codes are on the limits page.
Use a development tenant
Run these tests against a development or test tenant, never against production. A passwordless login creates a user the first time an address signs in, so a test that uses a new address each time creates a new user each time. A development tenant lets you delete them without worry. Two practical points:
- Make sure the passwordless email connection is enabled for the application the test uses, and that the application's allowed callback URLs include your test app's URL.
- Auth0 applies rate limits to authentication and to email sending, and they can differ by plan and by endpoint. Check Auth0's current rate-limit documentation before you plan a large suite, and keep the number of logins per run low. Where a test only needs a signed-in user and not the login page itself, create a session once and reuse it with Playwright's storage state instead of logging in for every test.
Also check that your tenant's email provider is configured. Auth0's built-in test email sending is meant for trying things out and is limited, so a tenant that sends real volume uses its own provider. That configuration is outside this guide.
The helper
Save this as tests/inbox.js. It needs Node 18 or newer for the built-in fetch.
const API = "https://assertinbox.com";
const KEY = process.env.ASSERTINBOX_KEY;
const NAMESPACE = process.env.ASSERTINBOX_NAMESPACE;
if (!KEY || !NAMESPACE) {
throw new Error("Set ASSERTINBOX_KEY and ASSERTINBOX_NAMESPACE");
}
let counter = 0;
function newInbox(label) {
const tag = (label + "-" + Date.now().toString(36) + "-" + ++counter + "-" + Math.random().toString(36).slice(2, 6)).toLowerCase();
return { tag, email: NAMESPACE + "." + tag + "@assertinbox.com" };
}
async function waitForEmail(tag, filters = {}) {
const params = new URLSearchParams({ tag, timeout: "25", ...filters });
for (let attempt = 0; attempt < 3; attempt++) {
const res = await fetch(API + "/v1/wait?" + params, {
headers: { Authorization: "Bearer " + KEY },
});
if (res.status === 200) return res.json();
if (res.status !== 408) throw new Error("AssertInbox returned HTTP " + res.status + " for tag " + tag);
}
throw new Error("No matching email arrived for tag " + tag);
}
async function deleteEmails(tag) {
await fetch(API + "/v1/messages?tag=" + encodeURIComponent(tag), {
method: "DELETE",
headers: { Authorization: "Bearer " + KEY },
});
}
module.exports = { newInbox, waitForEmail, deleteEmails };
The test
Save this as tests/auth0-passwordless.spec.js. The app under test starts the login when you click its sign-in button, which redirects to Auth0. The locators on the Auth0 page use roles and labels, which are more stable than CSS classes, but the Universal Login page can be customised and its text can change between versions and languages. If the test cannot find an element, open the page in a browser and adjust the label or button names to what your tenant shows.
const { test, expect } = require("@playwright/test");
const { newInbox, waitForEmail, deleteEmails } = require("./inbox");
const APP = process.env.APP_URL || "http://localhost:3000";
test("signs in with the emailed Auth0 code", async ({ page }) => {
const inbox = newInbox("auth0");
try {
await page.goto(APP);
await page.getByRole("button", { name: /log in|sign in/i }).click();
await page.waitForURL(/auth0\.com|\/u\/login/);
await page.getByLabel(/email/i).fill(inbox.email);
await page.getByRole("button", { name: /continue/i }).click();
const mail = await waitForEmail(inbox.tag);
expect(mail.otp).toMatch(/^[0-9]{4,8}$/);
await page.getByLabel(/code/i).fill(mail.otp);
await page.getByRole("button", { name: /continue|log in|submit/i }).click();
await page.waitForURL(APP + "/**");
await expect(page.getByText(inbox.email)).toBeVisible();
} finally {
await deleteEmails(inbox.tag);
}
});
The test does not filter the wait by subject because the subject of the Auth0 email is part of your tenant's email template, which you may have changed. The tag already isolates the message: only mail sent to this test's address can match. If your tenant sends more than one email to a new user, add a subject filter that matches the template you use.
Wrong code
A negative test is cheap and valuable: the login must refuse a wrong code and show an error without signing the user in. Use a code that cannot match, and be aware that repeated wrong codes may lock the attempt. Auth0 documents its limits on attempts, so keep this to a single wrong try per test.
test("rejects a wrong code", async ({ page }) => {
const inbox = newInbox("auth0-wrong");
try {
await page.goto(APP);
await page.getByRole("button", { name: /log in|sign in/i }).click();
await page.waitForURL(/auth0\.com|\/u\/login/);
await page.getByLabel(/email/i).fill(inbox.email);
await page.getByRole("button", { name: /continue/i }).click();
const mail = await waitForEmail(inbox.tag);
const wrong = mail.otp === "000000" ? "111111" : "000000";
await page.getByLabel(/code/i).fill(wrong);
await page.getByRole("button", { name: /continue|log in|submit/i }).click();
await expect(page.getByText(/wrong|invalid|incorrect/i)).toBeVisible();
await expect(page).not.toHaveURL(APP + "/**");
} finally {
await deleteEmails(inbox.tag);
}
});
The test still waits for the real email first. Without it, the app might never have started a passwordless flow, and a wrong code would be rejected for the wrong reason.
Reusing a session
Most tests in a suite are about your app, not about logging in. Run the login test above once, save the browser state with page.context().storageState({ path: "auth.json" }), and let the other tests start from that file through the storageState option in playwright.config.js. That keeps the number of Auth0 logins, and of emails, small. Keep auth.json out of version control; it contains session cookies.
Running in CI
- Set
ASSERTINBOX_KEYas a secret andASSERTINBOX_NAMESPACEas an ordinary variable. Never print them. - Run login tests in a single worker or in a small number of workers, to stay inside Auth0's limits.
- Fail with readable messages: the helper reports the HTTP status and the tag, never the key.
Troubleshooting
- The wait times out. Look at the Auth0 tenant logs for the login attempt. An email provider that is not configured, a disabled connection and a rate limit all show up there.
- The Auth0 page is not found by the locators. The Universal Login page text differs. Adjust the label and button names to what your tenant shows.
- The test is redirected back with an error. The callback URL is not in the application's allowed callback URLs.
- HTTP 429 from AssertInbox. The daily allowance is 2,000 requests per key.
Retry-Aftergives the seconds until 00:00 UTC.
Next steps
Read Reliable email tests for unique tags, retries and cleanup. Related guides: Supabase Auth email OTP, Auth.js magic link and Keycloak. The wait filters are described in the docs, and the client files wrap the helper for Node.js and Python.
Free beta, run by AI agents
This service is in free beta and is operated by AI agents under human supervision. Messages are deleted after 24 hours.