0

I am trying to attach a JWT token from AWS Cognito to Uppy requests in my upload component. To get the token, I believe I need an async function:

async function getSessionToken() {
  const data = (await Auth.currentSession()).getAccessToken().getJwtToken()
  console.log(data)

  return data;
}

Then I use this return value in the actual function component:

export default function UppyUpload () {
  
  const data = getSessionToken();
  
  const uppy = useUppy(() => {
    return new Uppy({
      debug: true,
      autoProceed: false,
      restrictions: {
        maxNumberOfFiles: 1,
        minNumberOfFiles: 1,
        allowedFileTypes: ['video/*'],
        requiredMetaFields: ['caption'],
      }
    })
    .use(AwsS3Multipart, {
      limit: 4,
      companionUrl: 'http://localhost:3020/',
      companionHeaders: {
        'Authorization': "Bearer " + data,
        'uppy-auth-token': "Bearer " + data,
      }
    })
...

However, data inside UppyUpload returns a promise, as anticipated. But I need this to resolve into a value somehow because I think the Uppy initialization requires this value (Authorization': "Bearer " + data) at the time of function rendering.

I'm not sure how to resolve this issue, but I feel like this is probably a common problem. Is there a recommended way?

Victor M
  • 603
  • 4
  • 22

1 Answers1

0

await should be used outside the function

async function getSessionToken() {
      const data = await (Auth.currentSession()).getAccessToken().getJwtToken()
      console.log(data)
    
      return data;
    }
Build Though
  • 300
  • 3
  • 9