AssertInbox

Test Firebase Authentication email-link sign-in with Playwright

Firebase Authentication can sign a user in with a link sent by email, with no password. The app calls sendSignInLinkToEmail, Firebase emails a link, and the app finishes the sign-in with signInWithEmailLink when the user opens it. This guide tests that flow end to end in Playwright Test (plain JavaScript). The test enters a unique address, waits for the email with the subject and after filters, opens the magic_link in the same browser context and asserts the signed-in state. A second test covers the link opened in a browser that never asked for it.

What your app and project need

The app uses the Firebase web SDK (v9 or newer, modular API). It calls sendSignInLinkToEmail(auth, email, actionCodeSettings) with a url that points back to a page of your app, and with handleCodeInApp set to true. On that page it checks isSignInWithEmailLink(auth, window.location.href) and calls signInWithEmailLink(auth, email, window.location.href). In the Firebase console, the Email/Password provider must have the email link option switched on, and the domain of the app under test must be in the list of authorized domains. Check the exact setup steps in the Firebase documentation for your SDK version.

Use a separate Firebase project for tests, not your production project. Firebase limits how many sign-in emails a project can send in a day; read the quotas in Firebase's own documentation before you run tests in CI. If you use the Firebase Emulator Suite, no real email is sent, so this guide applies to a real test project.

Why the same browser matters

The typical app saves the address in window.localStorage when it sends the link, because the sign-in page needs the address to finish. When the user opens the link in the same browser, the page finds the address and signs the user in at once. When the link is opened in another browser or device, the page cannot find it and must ask the user to type the address again. That is why the first test below opens the link with the same page, which shares storage with the page that requested it, and the second test uses a new browser context, which starts with empty storage. How your app stores the address, and what it shows when it is missing, is up to your code; adjust the second test to match it.

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, links and magic_link. Messages are deleted after 24 hours. The magic_link field holds 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/firebase-email-link.spec.js. Set APP_URL to the app under test. The labels, button names and texts are placeholders; replace them with those of your app. The subject filter sign in is also an assumption: use a word from the subject of your Firebase email template.

const { test, expect } = require("@playwright/test");
const { newInbox, waitForEmail, latestId, deleteEmails } = require("./inbox");

const APP = process.env.APP_URL || "http://localhost:3000";

test.describe("Firebase email-link sign-in", () => {
  test.setTimeout(120000);
  let inbox;

  test.beforeEach(() => {
    inbox = newInbox("firebase");
  });

  test.afterEach(async () => {
    await deleteEmails(inbox.tag);
  });

  test("the emailed link signs the user in", async ({ 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 sign-in link" }).click();
    await expect(page.getByText("Check your email")).toBeVisible();

    const mail = await waitForEmail(inbox.tag, { subject: "sign in", ...(before ? { after: before } : {}) });
    expect(mail.magic_link).toBeTruthy();

    // Same page, same browser context: the app saved the address in localStorage.
    await page.goto(mail.magic_link);
    await page.waitForURL(APP + "/**");
    await expect(page.getByText("Signed in")).toBeVisible();
    await expect(page.getByText(inbox.email)).toBeVisible();
  });

  test("opening the link in a fresh context asks for the address", async ({ page, browser }) => {
    const before = await latestId(inbox.tag);
    await page.goto(APP + "/login");
    await page.getByLabel("Email").fill(inbox.email);
    await page.getByRole("button", { name: "Send sign-in link" }).click();
    const mail = await waitForEmail(inbox.tag, { subject: "sign in", ...(before ? { after: before } : {}) });

    const other = await browser.newContext();
    const otherPage = await other.newPage();
    await otherPage.goto(mail.magic_link);
    await otherPage.waitForURL(APP + "/**");
    await expect(otherPage.getByLabel("Confirm your email")).toBeVisible();
    await otherPage.getByLabel("Confirm your email").fill(inbox.email);
    await otherPage.getByRole("button", { name: "Confirm" }).click();
    await expect(otherPage.getByText("Signed in")).toBeVisible();
    await other.close();
  });
});

Both tests ask for a new email at a new address, so they do not depend on each other and can run in parallel.

What the link does

The link in the email points at a Firebase-hosted handler for your project. Opening it checks the one-time code and redirects the browser to the url from your action code settings, with the sign-in parameters attached. That is why the tests wait for a URL under APP_URL before they assert anything. If the wait fails, check that the continue URL is on the authorized domains list of the project. Open each link only once: it is single-use, and a mail scanner on your side must not follow it before the test does.

Troubleshooting

Next steps

Read Reliable email tests for unique tags, retries and cleanup. Related guides: Playwright email verification, Supabase Auth email tests and Auth.js magic-link 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.