https://cypress.io logo
Auth0 login page appears even though programmatic ...
# i-need-help
l
I was hoping you guys could lend me a hand here. The company I work at has a hub page with a product grid where if you click on a button it will take you to that product. The hub page is hub.com Any product you click on that is on the hub page's product grid will take you to hub.com/productname Access to hub.com and all products is granted through Auth0 authentication. In Cypress we have a
before()
hook that does the following:
Copy code
before(() => {
  cy.restoreLocalStorage();
  cy.loginAndRedirect();
  cy.saveLocalStorage();
This is `loginAndRedirect()`:
Copy code
Cypress.Commands.add('loginAndRedirect', () => {
  Cypress.log({ name: 'log in with Auth0' })
  const authVariables = getAuth0Variables();

  const username = authVariables.username;
  const password = authVariables.password;
  const audience = authVariables.audience;
  const scope = 'openid profile email'
  const client_id = authVariables.client_id;
  const client_secret = authVariables.client_secret;

  return cy.request({
    method: 'POST',
    url: `${authVariables.url.replace(/\/+$/, "")}/oauth/token`,
    body: {
      grant_type: 'password',
      username,
      password,
      audience,
      scope,
      client_id,
      client_secret,
    },
  }).then(({ body: { access_token, expires_in, id_token } }) => {
    localStorage.setItem('access_token', access_token);
    localStorage.setItem(`@@auth0spajs@@::${client_id}::${audience}::${scope}`, JSON.stringify({
      body: {
        client_id,
        access_token,
        id_token,
        scope,
        expires_in,
        decodedToken: {
          user: jwt_decode(id_token),
        },
      },
      expiresAt: Math.floor(Date.now() / 1000) + expires_in,
    }))
    return cy.visit('/?cypress');
  });
});
My spec file looks like this:
Copy code
describe('Check if blablabla', () => {
  const hubPage = new HubPage();
  const companyProductPage = new CompanyProductPage();


  it('Loads Hub page and clicks on company product', () => {
    hubPage.waitForHubToLoad();
    hubPage.goToProductPageFromGrid(ProductNames.companyProduct); 
    console.log(localStorage.getItem('access_token'))
    mapperPage.waitUntilLoaded();
    mapperPage.configureAuthority(specSettings.authorityId);
  });
The spec fails. After it clicks the company product from the product grid it visits
hub.com/productname
but the Auth0 login page appears! I have no idea how to fix this. It should just load the product without asking for authentication again. What I have tried so far: 1. Set
testIsolation
to
false
2. Logged the
access_token
after in the spec file to ensure the access token persists
7 Views