Api data factory and rest api - I.have with bearer token

I have a working REST api with bearer authentication
I want to use the API data factory with the I.have(‘xxx’) factory construct but I need to be able to provide the bearer token somehow.

For the plain rest api calls, I have 2 additional headers in the config that I pass in to the
I.sendGetRequest(url, config)

As the bearer token is dynamically generated, I can’t provide the headers in the codecept.conf.js file.

I was thinking the constructor for the apidatafactory should do the authentication and have the custom headers added to the request, but I’m kinda stuck in setting up a custom helper cloned from apidatafactory

You can mutate requests in ApiDataFactory config:

ApiDataFactory: {
  endpoint: process.env.URL,
  cleanup: true,
  onRequest: async (request) => {
    authToken = await codeceptjs.container.helpers('Playwright').executeScript( () => localStorage.getItem('authToken') )
    request.headers = {
      ...request.headers,
      'Authorization': `Token ${authToken}`
    }
  },
  factories: {
    ...
  }
},