Cypress.Commands.add('login', () => {
if (Cypress.env('stage') === 'local') {
window.localStorage.setItem('dhuiToken', Cypress.env('localToken'));
cy.visit('/');
} else {
cy.request({
method: 'GET',
url: '/',
followRedirect: false,
}).then((response) => {
const { location: redirectLocation } = response.headers;
const state = decodeURIComponent(
redirectLocation
// @ts-expect-error
.substring(redirectLocation.indexOf('state'))
.split('=')[1]
.split('&')[0]
);
const optionsSessionToken = {
method: 'POST',
url: Cypress.env('session_token_url'),
body: {
username: Cypress.env('username'),
password: Cypress.env('password'),
options: {
warnBeforePasswordExpired: 'true',
multiOptionalFactorEnroll: 'true',
},
},
};
cy.request(optionsSessionToken).then((responseWithSession) => {
const { sessionToken } = responseWithSession.body;
const qs = {
client_id: Cypress.env('client_id'),
state,
redirect_uri: Cypress.env('redirect_uri'),
response_type: 'code',
scope: 'openid email offline_access',
access_type: 'offline',
sessionToken,
};
cy.request({
method: 'GET',
url: Cypress.env('auth_token_url'),
form: true,
followRedirect: false,
qs,
}).then(({ redirectedToUrl }) => {
const accessToken = redirectedToUrl
.substring(redirectedToUrl.indexOf('token'))
.split('=')[1]
.split('&')[0];
cy.wrap(accessToken).as('Token');
cy.visit(decodeURIComponent(redirectedToUrl));
});
});
});
}
}); and this is my login command, and trying ti implement