Skip to content

Instantly share code, notes, and snippets.

@toresbe
Last active November 2, 2022 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toresbe/a8f4c94562c0cb3d222eb630f28ff560 to your computer and use it in GitHub Desktop.
Save toresbe/a8f4c94562c0cb3d222eb630f28ff560 to your computer and use it in GitHub Desktop.
Replacing withCredentials in use-tus
import { useTus } from "use-tus"
// withCredentials used to include credentials with use-tus.
// But the documentation to do so isn't totally obvious.
// Discussion: https://github.com/tus/tus-js-client/issues/167#issuecomment-623073224
// Documentation: https://github.com/tus/tus-js-client/blob/14c3634e0ccd45973e90a3e460ca5e749f630107/docs/api.md
const { upload, setUpload } = useTus()
// Before, you could do this:
setUpload(file, {
withCredentials: true,
...
})
// But withCredentials has been removed. Now I do this:
setUpload(file, {
onBeforeRequest: (req) => {
const xhr = req.getUnderlyingObject() as XMLHttpRequest
xhr.withCredentials = true
xhr.setRequestHeader("X-CSRF-Token", csrfToken) // not relevant to the use case, but possibly useful
},
...
})
// Hope this was useful to someone. :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment