Hi, I’m trying to implement e2e test with cypress into an existing Angular 11 app.
I’m using the package auth0-angular 1.8.
I get the token from auth0 and I create the cookie but it seems that the sdk didn’t find any data in cookie/local storage and always redirect to auth0 login page.
Here is my commands.ts :
Cypress.Commands.add('login', (username:string, password:string) => {
Cypress.log({name: 'loginViaAuth0'})
const client_id = Cypress.env('auth_client_id');
const audience = `https://${Cypress.env('auth_domain')}/api/v2/`;
const rscope = 'openid profile email offline_access';
const options = {
method: 'POST',
url: `https://${Cypress.env('auth_domain')}/oauth/token`,
headers: {'content-type': 'application/x-www-form-urlencoded'},
body: {
grant_type: 'password',
username: username,
password: password,
audience: audience,
scope: rscope,
client_id: client_id
},
};
cy.request(options).then(response => {
const {scope, access_token, id_token, token_type, expires_in, refresh_token} = response.body;
cy.window().then(win => {
const payload = JSON.parse(Buffer.from(id_token.split('.')[1], 'base64').toString('ascii'));
win.localStorage.setItem(
`@@auth0spajs@@::${client_id}::${audience}::${rscope}`,
JSON.stringify({
body: {
client_id,
access_token,
id_token,
scope,
expires_in,
token_type,
refresh_token,
decodedToken: {
claims: Object.assign({}, payload, {__raw: id_token}),
encoded: {
header: id_token.split('.')[0],
payload: id_token.split('.')[1],
signature: id_token.split('.')[2],
},
header: JSON.parse(Buffer.from(id_token.split('.')[0], 'base64').toString('ascii')),
user: {
email: payload.email,
email_verified: payload.email_verified,
name: payload.name,
nickname: payload.nickname,
picture: payload.picture,
sub: payload.sub,
updated_at: payload.updated_at,
},
},
audience,
},
expiresAt: Math.floor(Date.now() / 1000) + expires_in,
})
);
});
cy.setCookie(`auth0.${client_id}.is.authenticated`, 'true');
});
});
and then my login.ts :
describe('login', () => {
it('should successfully log into our app', () => {
cy.login(Cypress.env('auth_username'), Cypress.env('auth_password'))
.then(() => {
cy.visit('/login?state=some-random-sate');
})
})
});
What is it wrong or missing ?
Thanks !