Test Supabase Auth email OTP and magic-link sign-in with Playwright
Supabase Auth can sign a user in with an emailed one-time code or with a magic link. Which one the user gets depends on the email template of your project. This guide tests both paths end to end in Playwright Test (plain JavaScript) against an app that uses supabase-js v2. The test enters a unique address, waits for the email with the subject and after filters, then either types the 6-digit otp into the app or opens the magic_link, and asserts the signed-in state.
What your app and project need
The app calls supabase.auth.signInWithOtp({ email }) and, for the code path, supabase.auth.verifyOtp({ email, token, type: "email" }). The email template under Authentication in the Supabase dashboard decides what the message contains: the {{ .Token }} variable puts the code in the email, and {{ .ConfirmationURL }} puts the link in it. A template can contain both, which lets one email serve both tests. Check the template syntax, the redirect URL allow-list and the code length in the Supabase documentation for your project.
Use a test project, not your production project. Supabase limits how many auth emails a project can send. Read the auth email rate limits and the custom SMTP settings in Supabase's own documentation before you run tests in CI, and configure your own SMTP server for a project that tests often. Inbox addresses from this service can be used with any sender.
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, links and magic_link. Messages are deleted after 24 hours. The otp field holds the code found in the message, and magic_link the link most likely to sign the user in.
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 CI secrets.
curl -s -X POST https://assertinbox.com/v1/keys
# {"api_key":"ai_...","namespace":"k3x9a1bz","address_template":"k3x9a1bz.<tag>@assertinbox.com"}
Export both values for the tests: ASSERTINBOX_KEY is the api_key and ASSERTINBOX_NAMESPACE is 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 listed in the limits page.
The helper
Save this as tests/inbox.js. It needs Node 18 or newer for the built-in fetch. waitForEmail accepts the filters and retries on a 408 timeout, and latestId returns the id of the newest message for a tag, or undefined when there is none.
const API = "https://assertinbox.com";
const KEY = process.env.ASSERTINBOX_KEY;
const NAMESPACE = process.env.ASSERTINBOX_NAMESPACE;
function headers() {
return { Authorization: "Bearer " + KEY };
}
function newInbox(prefix) {
const random = Math.random().toString(36).slice(2, 8);
const tag = (prefix + "-" + Date.now() + "-" + random).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: headers() });
if (res.status === 200) return res.json();
if (res.status !== 408) throw new Error("AssertInbox returned HTTP " + res.status);
}
throw new Error("No matching email arrived for tag " + tag);
}
async function latestId(tag) {
const res = await fetch(API + "/v1/messages?tag=" + encodeURIComponent(tag), { headers: headers() });
if (!res.ok) throw new Error("AssertInbox returned HTTP " + res.status);
const body = await res.json();
return body.messages.length ? body.messages[0].id : undefined;
}
async function deleteEmails(tag) {
await fetch(API + "/v1/messages?tag=" + encodeURIComponent(tag), { method: "DELETE", headers: headers() });
}
module.exports = { newInbox, waitForEmail, latestId, deleteEmails };
The tests
Save this as tests/supabase-auth.spec.js. Set APP_URL to the app under test. The labels, button names and the signed-in text are placeholders; replace them with those of your app. The subject filter confirm is also an assumption: use a word from the subject you configured.
const { test, expect } = require("@playwright/test");
const { newInbox, waitForEmail, latestId, deleteEmails } = require("./inbox");
const APP = process.env.APP_URL || "http://localhost:3000";
test.describe("Supabase Auth email sign-in", () => {
test.setTimeout(120000);
let inbox;
test.beforeEach(() => {
inbox = newInbox("supabase");
});
test.afterEach(async () => {
await deleteEmails(inbox.tag);
});
async function requestEmail(page) {
const before = await latestId(inbox.tag);
await page.goto(APP + "/login");
await page.getByLabel("Email").fill(inbox.email);
await page.getByRole("button", { name: "Send code" }).click();
return waitForEmail(inbox.tag, { subject: "confirm", ...(before ? { after: before } : {}) });
}
test("the 6-digit code signs the user in", async ({ page }) => {
const mail = await requestEmail(page);
expect(mail.otp).toMatch(/^\d{6}$/);
await page.getByLabel("Code").fill(mail.otp);
await page.getByRole("button", { name: "Verify" }).click();
await expect(page.getByText("Signed in")).toBeVisible();
});
test("a wrong code is rejected", async ({ page }) => {
const mail = await requestEmail(page);
const wrong = mail.otp === "000000" ? "111111" : "000000";
await page.getByLabel("Code").fill(wrong);
await page.getByRole("button", { name: "Verify" }).click();
await expect(page.getByText(/invalid|expired/i)).toBeVisible();
});
test("the magic link signs the user in", async ({ page }) => {
const mail = await requestEmail(page);
expect(mail.magic_link).toBeTruthy();
await page.goto(mail.magic_link);
await page.waitForURL(APP + "/**");
await expect(page.getByText("Signed in")).toBeVisible();
});
});
If your template contains only the code or only the link, keep just the matching tests. Both tests start from the same helper, so the second test simply asks for a new email at a new address.
The OTP path
The code test asserts the format before it types the code: six digits in this example. If your project is configured for another length, change the pattern. The wrong-code test sends a real email and then submits a code that cannot be the right one, to prove that the app shows an error and does not sign the user in. It reads the email only to know the real code, so that it can pick a different one.
The magic-link path
The link points at your Supabase project. Opening it verifies the token and redirects the browser to the redirect URL of your app, with the session in the URL. The redirect target must be on the allow-list of your project, or Supabase sends the browser to the site URL instead. That is why the test waits for a URL under APP_URL before it asserts the signed-in text. If the wait for that URL fails, check the redirect URL allow-list first, then the site URL setting.
Troubleshooting
- The wait times out. Check the Supabase auth logs and your project's email rate limit. A project that sent many emails recently may refuse to send more for a while. If the email arrived with a different subject, change the filter.
otpis null. The template does not contain{{ .Token }}, or the code is formatted in a way the extractor does not recognise. Printmail.textand adjust the template.magic_linkis null. The template does not contain{{ .ConfirmationURL }}.- The page does not sign in after the link. Open the link only once: tokens are single-use, and an email scanner on your side must not follow the link before the test does.
- HTTP 429 from AssertInbox. The key used its 2,000 requests for the day.
Retry-Aftergives the seconds until 00:00 UTC.
Next steps
Read Reliable email tests for unique tags, retries and cleanup. Related guides: Playwright email verification, Auth.js magic-link tests and password reset tests. The wait filters are described in the docs.
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.