← Back to library
Cypressauthintermediatev10+
Login via cy.request (skip UI)
Authenticate by hitting the API directly — much faster than typing through the login form.
1// cypress/support/commands.js
2Cypress.Commands.add('loginByApi', (email, password) => {
3 cy.request({
4 method: 'POST',
5 url: '/api/login',
6 body: { email, password },
7 }).then(({ body }) => {
8 // Store the token however your app uses it
9 window.localStorage.setItem('auth_token', body.token);
10 });
11});
12
13// Usage in tests:
14// cy.loginByApi('user@test.com', 'secret');
15// cy.visit('/dashboard'); // already authenticated#login#api#cy.request#session