Skip Login With Saved Token in Cypress
- Radek Stolarczyk
- 15 hours ago
- 1 min read

Typing email + password in every test is slow and noisy. Cypress can skip the login screen completely by grabbing a token via API and placing it into localStorage before the app loads.
This is a practical reference showing the before vs after, plus a reusable custom command.
Why This Helps
UI login usually costs ~8 seconds per test run (open page, fill fields, submit, wait UI).
Token-based login can cut it to ~5 seconds because it avoids the whole form flow.
Tests become cleaner because login steps don’t distract from the real scenario.
How It Works
The test requests a JWT token from the login API, then stores it in window.localStorage using cy.visit(..., { onBeforeLoad }) so the app boots already authenticated.
Custom Command: Login via API + Store Token
cypress/support/commands.js
The key name matters: it must match what the application reads (jwtToken in my case).
Before: Repeating Login + Token Creation in Test
This version:
logs in via API to create data
logs in again via UI to reach authenticated pagesIt works, but it’s repetitive.
After: One Command = Auth + Token Ready + App Opened
This version:
calls one command
gets a token ready for API calls
opens the app already logged inLess code, less waiting.
Notes and Best Practices
Always validate API status codes before using data (200, 201, etc.).
Store the token before the app loads (onBeforeLoad) so the app initializes authenticated.
Keeping the token as an alias (cy.wrap(token).as('token')) makes it reusable without repeating API login calls.
For complex auth providers (Okta/Auth0/etc.), Cypress has official guides under authentication testing: https://docs.cypress.io/app/guides/authentication-testing/auth0-authentication