> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.doc-detective.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.doc-detective.com/_mcp/server.

# Troubleshoot sign-in tests

> Fix sign-in tests that end up on the sign-in page, fail to load a saved session or cookie, or have their one-time code rejected.

Use this page when a sign-in test fails, or when a reused session or cookie leaves a test on the wrong page. Each entry starts with what you see in the test results.

## The test shows the sign-in page

The session loaded, but the server didn't accept it. Sessions expire, and a sign-out or a password change revokes them on the server. To fix it:

1. Check that your sign-in spec runs in `beforeAll` and sets `"overwrite": true`, so each run saves a fresh session.
2. Check that no test selects **Sign out** or changes the account's password. That ends the session for every test that loads the same file. Give that test its own sign-in.
3. Add a `find` for an element that only a signed-in reader sees, such as **Sign out**, after each load and before each `screenshot`. The test then fails at that step instead of taking a screenshot of the sign-in page.

## The session loads, but the page still shows the sign-in form

Most apps read their session only when a page loads, so the page that was open during `loadState` or `loadCookie` doesn't know about it yet. Add a `goTo` after the load step, to the page you want to test:

```json
{
  "steps": [
    { "goTo": "https://app.example.com" },
    { "loadState": { "path": "auth.json", "directory": "../.auth" } },
    { "goTo": "https://app.example.com/billing" },
    { "find": "Sign out" }
  ]
}
```

## `loadState` or `loadCookie` fails because the right site isn't open

`loadState` and `loadCookie` set cookies and storage on the open site, so they need that site open first. With no site open, `loadState` fails with the first message. When the open page's host doesn't fit the cookie, `loadCookie` fails with the second, and the host is empty when no site is open:

```text
Couldn't load state: no site is open. Use `goTo` first.
```

```text
Couldn't load cookie: its domain `app.example.com` doesn't match the open page's host `example.com`. Use `goTo` to open that site first.
```

Add a `goTo` to the site the file was saved from before the load step, as in the preceding example.

## `loadState` has no data for the open site

```text
Couldn't load state: `/home/runner/work/docs/.auth/auth.json` has no data for `https://example.com`. Use `goTo` to open that site first.
```

The file holds cookies and storage for another site. Check that:

* The `goTo` before `loadState` opens the same host that the sign-in spec saved from. `www.example.com` and `example.com` are different hosts, and `localStorage` also depends on `http` or `https` and the port.
* You saved a file for each site your tests open. One `saveState` saves one site. For a sign-in that passes through an identity provider, see [Sign in through single sign-on](/docs/test-docs/sso-sign-in).

## The state or cookie file doesn't exist

```text
Couldn't load state: `/home/runner/work/docs/.auth/auth.json` doesn't exist.
```

The path resolves to a different place than the sign-in spec saved to, or the sign-in didn't run. Check that:

* The sign-in spec is listed in `beforeAll`, and it passed.
* Each spec's `directory` points at the same folder. By default, a relative `directory` resolves against the spec file that contains the step, so specs in different folders need different `directory` values.

## The state or cookie file already exists

```text
Couldn't save state: `/home/runner/work/docs/.auth/auth.json` exists. Set `overwrite` to `true` to replace it.
```

Add `"overwrite": true` to the `saveState` or `saveCookie` step.

## A saved cookie has expired

```text
Couldn't load cookie: `session_id` has expired.
```

`loadCookie` doesn't set a cookie past its expiry. Sign in again in a `beforeAll` spec on every run, rather than keeping a cookie file between runs. `loadState` skips expired cookies instead. When something else in the file still fits the site, an expired session shows up as [the sign-in page in your test](#the-test-shows-the-sign-in-page). When nothing else fits, `loadState` fails with [no data for the open site](#loadstate-has-no-data-for-the-open-site).

## A one-time code is rejected

The app says the code from `totp()` is wrong or expired. Check these causes in order:

* **The clock is off.** The code depends on the time, so the machine that runs the test needs an accurate clock. Turn on network time sync. Containers and virtual machines that were paused or restored can drift.
* **The secret is out of date.** Re-enrolling the authenticator, or resetting MFA on the account, creates a new key. Update the `TOTP_SECRET` CI secret and your `.env` file with the new key.
* **The authenticator uses other settings.** `totp()` generates SHA-1, 6-digit, 30-second codes. An app that asks for 8 digits or uses another period or algorithm rejects them.
* **The code was already used.** Many apps accept each code only once. Two tests or CI jobs that sign in within the same 30 seconds can get the same code, and the app rejects the second. Sign in once in `beforeAll` and [reuse the session](/docs/test-docs/reuse-a-session). Give jobs that run at the same time their own test accounts.

When the secret isn't valid base32, the step that generates the code fails with a message that ends like this:

```text
Couldn't generate a TOTP code: the secret isn't valid base32.
```

Check that the variable holds the setup key itself, letters `A`–`Z` and digits `2`–`7`, and not the whole `otpauth://` link.

## The app keeps its session in `sessionStorage`

`saveState` doesn't save `sessionStorage`, so the test is signed out after `loadState`, even with a fresh file. To fix it, choose one:

* Sign in during each test that needs the session, instead of in `beforeAll`.
* Ask your engineering team whether the test environment can keep the session in a cookie or `localStorage`. Also ask whether there's a test-only way to create a session. See [Ask your engineering and QA teams](/docs/test-docs/sign-in#ask-your-engineering-and-qa-teams).