Giter Site home page Giter Site logo

Comments (4)

Murderlon avatar Murderlon commented on July 21, 2024 1

Closing this in favor of the mentioned issue.

from uppy.

Murderlon avatar Murderlon commented on July 21, 2024

Hi, you can also do this and not destroy the instance:

  useEffect(() => {
    uppy.getPlugin('XHRUpload').setOptions({
      headers: {
        Authorization: `Bearer ${token}`,
      }
    })
  }, [token]);

The problem then is that it isn't retried. With tus you can do this:

import Uppy from '@uppy/core';
import Tus from '@uppy/tus';

new Uppy().use(Tus, {
  endpoint: '',
  async onBeforeRequest(req) {
    const token = await getAuthToken();
    req.setHeader('Authorization', `Bearer ${token}`);
  },
  onShouldRetry(err, retryAttempt, options, next) {
    if (err?.originalResponse?.getStatus() === 401) {
      return true;
    }
    return next(err);
  },
  async onAfterResponse(req, res) {
    if (res.getStatus() === 401) {
      await refreshAuthToken();
    }
  },
});

Ideally this would be consistent between uploaders.

from uppy.

lmyslinski avatar lmyslinski commented on July 21, 2024

yeah onBeforeRequest is exactly what I had in mind. I'll try this one out for now, thanks

from uppy.

Murderlon avatar Murderlon commented on July 21, 2024

I just saw this in the codebase:

queuedRequest = this.requests.run(() => {
// When using an authentication system like JWT, the bearer token goes as a header. This
// header needs to be fresh each time the token is refreshed so computing and setting the
// headers just before the upload starts enables this kind of authentication to work properly.
// Otherwise, half-way through the list of uploads the token could be stale and the upload would fail.
const currentOpts = this.getOptions(file)
Object.keys(currentOpts.headers).forEach((header) => {
xhr.setRequestHeader(header, currentOpts.headers[header])
})

So my useEffect suggestion in #5042 (comment) should be sufficient for now.

from uppy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.