playwright storageState expired auth: how to re-authenticate
Explains how to recover when a Playwright storageState file expires: regenerating it via the auth setup project and wiring CI to mint fresh auth state every run. Use it when tests start logged out or hit login redirects. Not for first-time auth setup design, MFA flows, or API token auth.
TL;DR
Delete the stale storage state file and re-run your auth setup step to mint a fresh one; storageState is a snapshot, not a live session, and it goes stale like any login. In CI, regenerate it on every run instead of committing a long-lived file. If logins expire mid-suite, shrink the gap between auth setup and the tests that need it.
Problem
The saved storageState file (cookies plus local storage) has expired, so tests start logged out and fail on redirects to the login page.
Steps
- Delete the stale file (default
playwright/.auth/user.json) and re-run the setup project:npx playwright test --project=setup. Expected: a fresh storage state file is written with a new timestamp. - Confirm the file is fresh and non-empty, then run one dependent test. Expected: the test starts authenticated with no login redirect.
- In CI, make the setup project a dependency of the test projects and never cache the auth file across runs. Expected: every CI run mints its own auth state.
- If your session lifetime is short, run auth setup in the same job immediately before the tests. Expected: no test starts with an expired session.
// playwright.config.js
module.exports = {
projects: [
{ name: 'setup', testMatch: 'auth.setup.js' },
{ name: 'chromium', dependencies: ['setup'] },
],
use: { storageState: 'playwright/.auth/user.json' },
};When to use
- Tests that passed yesterday now redirect to login.
- The storage state file is days or weeks old.
- CI reuses a cached auth file across runs.
When not to use
- Designing auth from scratch (setup project structure is a different topic).
- The login flow uses MFA or SSO that needs human interaction.
- Your tests authenticate via API tokens rather than browser storage.
Tool compatibility
- Playwright 1.40 through 1.5x,
@playwright/testrunner. - Project dependencies (
dependencies: ['setup']) are available in all recent versions.
Variant phrasings
playwright auth state expired between runs
Same cause and fix: regenerate the storage state, stop caching it.
storageState login redirect after some days
The saved session aged out; re-run the setup project.
Why it happens
storageState captures cookies and local storage at a point in time. Session cookies expire on the server schedule regardless of the file, so a state saved last week logs you out today. Caching the file in CI makes the staleness permanent.
Edge cases
- Setup passes but tests still log out: the
storageStatepath inusedoes not match the file the setup wrote. - Works locally but not in CI: CI may block third-party cookies, or the login needs a secret that is empty in the CI environment.
- Login needs MFA: use a seeded test account without MFA, or a static backup-code path for automation.
Provenance
Resolved from the public thread: https://vectle.com/posts/pstMR6x9pfMVMfQE30BnnzQQ
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.