Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events not being triggered #3661

Closed
vmdumitrache opened this issue Apr 25, 2022 · 3 comments
Closed

Events not being triggered #3661

vmdumitrache opened this issue Apr 25, 2022 · 3 comments
Assignees
Labels

Comments

@vmdumitrache
Copy link

I'm running into something which I believe could be a bug.
I am trying to implement Uppy and tus in a React Native application. The back-end implements JWT authentication, with access and refresh tokens.
As recommended in this comment, I am trying to rely on events to determine whether the back-back end responds with HTTP 401 so that I can attempt to renew the access token.
The bug appears to be that events are not firing. I presume it's all of them, but I know for certain that error and upload error have no effect.

My implementation looks something like this:

  async function startUpload() {
    const accessToken = await SecureStore.getItemAsync("token");
    if (imageList.length === 0) return;

    const uppy = new Uppy({
      debug: true,
    });

    uppy.use(Tus, {
      endpoint:
        "https://the-actual-endpoint.tld",
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
      retryDelays: [0, 1000, 3000, 5000],
    });

    uppy.on("error", async (error) => {
      console.log(error);
    });

    uppy.on("upload-error", async (file, error, response) => {
      console.log(file);
      console.log(error);
      console.log(response);
    });

    imageList.forEach((image) => {
      const name = image.uri.replace(/^.*[\\/]/, "");
      const imageData = {
        name,
        type: "image/png",
        data: image,
      };

      uppy.addFile(imageData);
    });

    uppy.upload().then((result) => {
      console.log("upload success response: ", result);
    });
  }

I've set a 10 second TTL for access tokens to ensure that by the time I attempt the upload, the token is definitely expired.
As debug is set to true, I am receiving these messages in the console:

[Uppy] [23:23:15] Using Core v2.1.8

[Uppy] [23:23:15] Using Tus v2.2.2

[Uppy] [23:23:15] Added file: 002bb5be-6103-47d2-84e6-ce30d9261284.png, uppy-002bb5be/6103/47d2/84e6/ce30d9261284/png-1d-1d-1d-1d-1e-image/png, mime type: image/png

[Uppy] [23:23:15] [Tus] Uploading...

[Uppy] [23:23:16] [Error: tus: unexpected response while creating upload, originated from request (method: POST, url: https://the-actual-endpoint.tld, response code: 401, response text: {"code":401,"message":"Expired JWT Token"}, request id: n/a)]

Thanks!

@Murderlon
Copy link
Member

Murderlon commented Apr 26, 2022

Just tested this in e2e and it works. Not sure how to reproduce or if this is actually an issue?

  it.only('should emit events when JWT token is expired', () => {
    cy.get('@file-input').attachFile(['images/cat.jpg', 'images/traffic.jpg'])

    const error = cy.spy()
    const uploadError = cy.spy()
    cy.window().then(({ uppy }) => {
      uppy.on('upload-error', uploadError)
      uppy.on('error', error)
    })

    cy.get('.uppy-StatusBar-actionBtn--upload').click()

    cy.intercept(
      { method: 'POST', pathname: '/files', times: 1 },
      { statusCode: 401, body: { code: 401, message: 'Expired JWT Token' } },
    ).as('patch')

    cy.wait('@patch').then(() =>  {
      expect(error).to.be.called
      expect(uploadError).to.be.called
    })
  })

@vmdumitrache
Copy link
Author

Hi Murderlon,

Thank you kindly for looking into this so promptly and many apologies for wasting your time!

It would appear that the issue I was having was caused by caches. I cleared the Metro cache and the events started working without a glitch.

On a separate note, I noticed that the upload-error event does not also return an ErrorResponse, but returns undefined. I do realise that, by definition, the latter is perfectly acceptable.

I applied a patch locally to also get the ErrorResponse and could gladly raise a PR for it if you think that would be OK. In situations where JWT authentication is used, I see how it could help. I'm more than happy to also open a separate issue for this, as it's different from what was initially described.

Thanks!

@Murderlon
Copy link
Member

I'm closing this then but PRs would be welcome for a potential fix of the response! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants