This is how I solved that in React:
1 - Create a command to log in:
// cypress/support/commands.js
Cypress.Commands.add('A0login', () => {
Cypress.log({
name: 'loginViaAuth0',
})
const audience = Cypress.env('auth_audience')
const client_id = Cypress.env('auth_client_id')
const scope = 'openid email profile offline_access'
const options = {
method: 'POST',
url: Cypress.env('auth_url'),
body: {
grant_type: 'http://auth0.com/oauth/grant-type/password-realm',
realm: 'Username-Password-Authentication',
username: Cypress.env('DEFAULT_USER'),
password: Cypress.env('DEFAULT_PASSWORD'),
audience,
scope,
client_id,
client_secret: Cypress.env('auth_client_secret'),
},
}
cy.request(options).then(({ body }) => {
const claims = jwt.decode(body.id_token)
const { nickname, name, picture, updated_at, sub, exp } = claims
const { access_token, id_token, token_type, expires_in, refresh_token } =
body
const item = {
body: {
access_token,
audience,
client_id,
id_token,
oauthTokenScope: scope,
expires_in,
refresh_token,
scope,
token_type,
decodedToken: {
claims,
user: {
nickname,
name,
picture,
updated_at,
sub,
},
},
},
expiresAt: exp,
}
window.localStorage.setItem(
`@@auth0spajs@@::${Cypress.env('auth_client_id')}::${Cypress.env(
'auth_audience',
)}::openid email profile offline_access`,
JSON.stringify(item),
)
})
})
This command makes a request to get a token, which is later decoded and stored in localstorage.
2 - You need to have Auth0 cache location set to 'localstorage'
3 - Just execute login before each test suit and persist the localStorage:
before(() => {
cy.A0login()
cy.visit('/')
cy.saveLocalStorage()
})
beforeEach(() => {
cy.restoreLocalStorage()
})
afterEach(() => {
cy.saveLocalStorage()
})
In my case cy.saveLocalStorage() and cy.restoreLocalStorage() are both custom commands.
The only problem you might find is that this authentication is via 'username-password', but thats not a big issue If you just want to test functionality of your app and components.