Your callback function returned a promise that never resolved

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);
});
}

Hi mate, I’m in the same situation. Could you resolve it? Thanks.

Hi, I am also facing same issue. Any update on this?

Are you seeing any errors in the dev console or the network events? Is the /authorize call failing?

Got resolved by adding code_challenge, state, code_challenge_method, which was commented earlier. Forgot to update the thread. Thanks for your quick response on checking.

1 Like

I am facing this exact same issue. Could you please go into more detail as to how you resolved it? I am completely stumped. Thanks.