Im setting up a suite of E2E tests using cypress. I have a react typescript app and I am trying to get this working with cypress. My major problem that I am facing is that I am using Auth0 as my authentication provider. This means I need to programmatically log the user in before each test. However I can't seem to get the correct method of doing this. When I try it calls the call back url but it doesn't seem to have authenticated the user because they just go to a blank page? I am using the @auth0/auth0-react package which has a hook for accessing all of the Auth0 data.
Cypress command:
import jwt_decode from 'jwt-decode';
Cypress.Commands.add('login', () => {
cy.log(`logging in as ${Cypress.env('auth_username')}`);
cy.request({
method: 'POST',
url: Cypress.env('auth_url'),
body: {
grant_type: 'password',
username: Cypress.env('auth_username'),
password: Cypress.env('auth_password'),
audience: Cypress.env('auth_audience'),
scope: 'openid profile email',
client_id: Cypress.env('auth_client_id'),
client_secret: Cypress.env('auth_client_secret'),
},
}).then(({ body }) => {
const claims = jwt_decode(body.id_token);
const { nickname, name, picture, updated_at, email, email_verified, sub, exp } = claims;
const item = {
body: {
...body,
decodedToken: {
claims,
user: {
nickname,
name,
picture,
updated_at,
email,
email_verified,
sub,
},
audience: Cypress.env('auth_audience'),
client_id: Cypress.env('auth_client_id'),
},
},
expiresAt: exp,
};
window.localStorage.setItem('auth0Cypress', JSON.stringify(item));
});
});