dunson062786 / login-programmatically-into-auth0-with-cypress

15 stars 2 forks source link

`default` Audience isn't necessarily the correct one #4

Open cchapman-tlcdeaf opened 4 years ago

cchapman-tlcdeaf commented 4 years ago

I was having trouble with the default Audience being incorrect since I had previously already had more that one Auth0 audience already set up for other projects. There was no error message, it just wasn't seeming to save the cookie correctly after programmatically logging in.

I originally had this code as from how it is in this repo

// /cypress/support/commands.js
      ...
      // Auth0 SPA SDK will check for value in cookie to get appState
      // add validate nonce (which has been removed for simplicity)
      const stateId = 'test'; // good enough for you local machine, but not for prod
      console.log(appState)
      cy.setCookie(
        `a0.spajs.txs.${stateId}`,
        encodeURIComponent(JSON.stringify({
          "appState": appState,
          "scope": "openid profile email",
          "audience": 'default',          // This was the wrong audience, since I already had one in Auth0 for something else
          "redirect_uri": "http://localhost:3000"
        }))
      ).then(() => {
        cy.visit(`/?code=test-code&state=${stateId}`);
      });

but I updated it to

      ...
      // Auth0 SPA SDK will check for value in cookie to get appState
      // add validate nonce (which has been removed for simplicity)
      const stateId = 'test'; // good enough for you local machine, but not for prod
      console.log(appState)
      cy.setCookie(
        `a0.spajs.txs.${stateId}`,
        encodeURIComponent(JSON.stringify({
          "appState": appState,
          "scope": "openid profile email",
          "audience": Cypress.env('auth_audience'),   // This did work with that variable set in cypress.env.json
          "redirect_uri": "http://localhost:3000"
        }))
      ).then(() => {
        cy.visit(`/?code=test-code&state=${stateId}`);
      });