title: Cypress: Re-use auth token across multiple API tests
body: <p>I have a Rest API which generates a token. This session token is used across multiple REST API's as an authorization <em>Bearer token</em>. I used this as reference: <a href="https://github.com/cypress-io/cypress-example-recipes/blob/master/examples/logging-in__jwt/cypress/integration/spec.js" rel="noreferrer">https://github.com/cypress-io/cypress-example-recipes/blob/master/examples/logging-in__jwt/cypress/integration/spec.js</a></p>
  
  <p>However, in that example, the function to generate token is embedded in the test. I tried to create a custom command for which should store locally but it is not being picked up by the test. Note that no return value is included in the custom command.</p>
  
  <p>My code below under <strong>support/commands.js</strong>:</p>
  
  <pre><code>let identity
  Cypress.Commands.add('postToken', () =&gt; {
      cy.request({
          method: 'POST',
          url: Cypress.env('api_identity_url'), //get from cypress.env.json
          form: true, //sets to application/x-www-form-urlencoded
          body: {
              grant_type: 'client_credentials',
              scope: 'xero_all-apis'
          },
          auth: {
              username: Cypress.env('api_identity_username'),
              password: Cypress.env('api_identity_password')
          }
      })
          .its('body')
          .then((response) =&gt; {
              identity = response
              window.localStorage.setItem('identity', JSON.stringify(identity))
              cy.log(identity.access_token)
          })
  })
  </code></pre>
  
  <p><strong>My test</strong>:</p>
  
  <pre><code>context('Check token details', () =&gt; {
    it('Check token', () =&gt; {
        cy.postToken()
        const bToken = window.localStorage.getItem('identity')
        cy.log(bToken)
    })
  })
  </code></pre>
  
  <p>When I run the test, the log shows <code>null</code> value for 'identity'. However, it shows the current value in the custom command where I placed <code>cy.log(identity.access_token)</code>
  I tried using <strong>cy.writeFile</strong> but I don't think this is a clean method. There must be some way data can be passed between functions, and different classes.</p>
  
  <p><strong>Sample JSON format:</strong></p>
  
  <pre><code>{
    "token": "&lt;this is the value I would like to use for other API's authorisation bearer token&gt;",
    "expires_in": 1200,
    "token_type": "Bearer"
  }
  </code></pre>