title: K6: Authorization in the Test Life Cycle
body: <p>I have been reading through the K6 documentation and am struggling to find a way to conform a K6 script to meet my needs. The scenario I imagine in my head:</p>
  <pre><code>// 1. init code
  export function setup() {
    // 2. setup code
    // The VU would get setup here with authentication once
  }
  export default function (data) {
    // 3. VU code
    // The VU would run this code a repeated amount of times
  }
  export function teardown(data) {
    // 4. teardown code
    // The VU would be deauthed here after the VU code has ran its course
  </code></pre>
  <p>Unfortunately, from what I've read here: <a href="https://k6.io/docs/using-k6/test-life-cycle" rel="nofollow noreferrer">https://k6.io/docs/using-k6/test-life-cycle</a>, my understanding is that the above scenario isn't possible. &quot;<code>setup</code> is called at the beginning of the test, after the init stage but before the VU stage (default function), and teardown is called at the end of a test, after the VU stage (<code>default</code> function). Therefore, VU number is 0 while executing the <code>setup</code> and <code>teardown</code> functions.&quot; I interpret this to mean that in the above scenario, all the VU's would use the same authentication to run the VU code instead of each VU getting it's own separate authentication.</p>
  <p>If I simply exclude the setup and teardown sections, then each VU would get new authentication on each loop:</p>
  <pre><code>export default function (data) {
    // 3. VU code
    // VU gets authenticated
    // VU does a bunch of stuff
    // VU clears authentication
  }
  </code></pre>
  <p>Is there a way to design K6 scripts with my intention (VU Authenticates once -&gt; VU code loop -&gt; Deauth at the end once)?</p>