From d87b30651bb987d20984ae3ec0250ec4c9e8fdfc Mon Sep 17 00:00:00 2001 From: jhz Date: Sun, 5 Feb 2023 20:29:11 +0800 Subject: [PATCH] added optional to set authorization header token --- src/components/fetch-wrapper.jsx | 7 +++++-- src/components/file-viewer.jsx | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/components/fetch-wrapper.jsx b/src/components/fetch-wrapper.jsx index 14f6d09c..89a794a8 100644 --- a/src/components/fetch-wrapper.jsx +++ b/src/components/fetch-wrapper.jsx @@ -10,7 +10,7 @@ function withFetching(WrappedComponent, props) { constructor(props) { // eslint-disable-line no-shadow super(props); this.state = {}; - this.xhr = this.createRequest(props.filePath); + this.xhr = this.createRequest(props.filePath, props.authToken); } componentDidMount() { @@ -28,9 +28,12 @@ function withFetching(WrappedComponent, props) { this.abort(); } - createRequest(path) { + createRequest(path, authToken=null) { let xhr = new XMLHttpRequest(); + if(authToken !== null) + xhr.setRequestHeader('Authorization', authToken); + if ('withCredentials' in xhr) { // XHR for Chrome/Firefox/Opera/Safari. xhr.open('GET', path, true); diff --git a/src/components/file-viewer.jsx b/src/components/file-viewer.jsx index f1a524e8..1adab117 100644 --- a/src/components/file-viewer.jsx +++ b/src/components/file-viewer.jsx @@ -85,6 +85,7 @@ class FileViewer extends Component { FileViewer.propTypes = { fileType: PropTypes.string.isRequired, filePath: PropTypes.string.isRequired, + authToken: PropTypes.string, onError: PropTypes.func, errorComponent: PropTypes.element, unsupportedComponent: PropTypes.element,