Hii Team,
I am working to programmatically login to Okta using Cypress. Getting error, Can you please help.
Cypress.Commands.add(‘loginByOktaApi’, (username, password) => {
cy.request({
method: ‘POST’,
url: https://${Cypress.env('okta_domain')}/api/v1/authn
,
body: {
username,
password,
},
}).then(({ body }) => {
const user = body._embedded.user
const config = {
issuer: `https://${Cypress.env('okta_domain')}/oauth2/default`,
clientId: Cypress.env('okta_client_id'),
redirectUri: 'http://localhost:3000/implicit/callback',
scope: ['openid', 'email', 'profile'],
}
const authClient = new OktaAuth(config)
console.log(authClient)
console.log(authClient.token.getWithoutPrompt({ sessionToken: body.sessionToken }))
return authClient.token
.getWithoutPrompt({ sessionToken: body.sessionToken })
.then(({ tokens }) => {
const userItem = {
token: tokens.accessToken.value,
user: {
sub: user.id,
email: user.profile.login,
given_name: user.profile.firstName,
family_name: user.profile.lastName,
preferred_username: user.profile.login,
},
}
window.localStorage.setItem('oktaCypress', JSON.stringify(userItem))
log.snapshot('after')
log.end()
},error=>{
console.log(error.message)
})
})
})
I am getting error
cy.then() timed out after waiting 4000ms.
Your callback function returned a promise that never resolved.
The callback function was:
({
body
}) => {
const user = body._embedded.user;
const config = {
issuer: https://${Cypress.env(‘okta_domain’)}/oauth2/default,
clientId: Cypress.env(‘okta_client_id’),
redirectUri: ‘http://localhost:3000/implicit/callback’,
scope: [‘openid’, ‘email’, ‘profile’]
};
const authClient = new _oktaAuthJs.OktaAuth(config);
console.log(authClient);
console.log(authClient.token.getWithoutPrompt({
sessionToken: body.sessionToken
}));
return authClient.token.getWithoutPrompt({
sessionToken: body.sessionToken
}).then(({
tokens
}) => {
const userItem = {
token: tokens.accessToken.value,
user: {
sub: user.id,
email: user.profile.login,
given_name: user.profile.firstName,
family_name: user.profile.lastName,
preferred_username: user.profile.login
}
};
window.localStorage.setItem(‘oktaCypress’, JSON.stringify(userItem));
log.snapshot(‘after’);
log.end();
}, error => {
console.log(error.message);
});
}