Skip to content

Commit

Permalink
added optional to set authorization header token
Browse files Browse the repository at this point in the history
  • Loading branch information
hozhaojung committed Feb 5, 2023
1 parent 6f9348c commit d87b306
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/components/fetch-wrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/components/file-viewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d87b306

Please sign in to comment.