Bug fix. Ensure we check for changed token in empty responses. Also ensure we send token before each upload (as it might change between uploads)

This commit is contained in:
Tim Donohue
2021-02-19 10:58:47 -06:00
parent 59676b7c52
commit 4a72cb033b

View File

@@ -114,8 +114,6 @@ export class UploaderComponent {
autoUpload: this.uploadFilesOptions.autoUpload, autoUpload: this.uploadFilesOptions.autoUpload,
method: this.uploadFilesOptions.method, method: this.uploadFilesOptions.method,
queueLimit: this.uploadFilesOptions.maxFileNumber, queueLimit: this.uploadFilesOptions.maxFileNumber,
// Ensure the current XSRF token is included in every upload request
headers: [{ name: XSRF_REQUEST_HEADER, value: this.tokenExtractor.getToken() }]
}); });
if (isUndefined(this.enableDragOverDocument)) { if (isUndefined(this.enableDragOverDocument)) {
@@ -140,6 +138,8 @@ export class UploaderComponent {
if (item.url !== this.uploader.options.url) { if (item.url !== this.uploader.options.url) {
item.url = this.uploader.options.url; item.url = this.uploader.options.url;
} }
// Ensure the current XSRF token is included in every upload request (token may change between items uploaded)
this.uploader.options.headers = [{ name: XSRF_REQUEST_HEADER, value: this.tokenExtractor.getToken() }];
this.onBeforeUpload(); this.onBeforeUpload();
this.isOverDocumentDropZone = observableOf(false); this.isOverDocumentDropZone = observableOf(false);
@@ -155,14 +155,15 @@ export class UploaderComponent {
}; };
} }
this.uploader.onCompleteItem = (item: any, response: any, status: any, headers: any) => { this.uploader.onCompleteItem = (item: any, response: any, status: any, headers: any) => {
// Check for a changed XSRF token in response & save new token if found
// NOTE: this is only necessary because ng2-file-upload doesn't use an Http service and therefore never
// triggers our xsrf.interceptor.ts. See this bug: https://github.com/valor-software/ng2-file-upload/issues/950
const token = headers[XSRF_RESPONSE_HEADER.toLowerCase()];
if (isNotEmpty(token)) {
this.saveXsrfToken(token);
}
if (isNotEmpty(response)) { if (isNotEmpty(response)) {
// Check for a changed XSRF token in response & save new token if found
// NOTE: this is only necessary because ng2-file-upload doesn't use an Http service and therefore never
// triggers our xsrf.interceptor.ts. See this bug: https://github.com/valor-software/ng2-file-upload/issues/950
const token = headers[XSRF_RESPONSE_HEADER.toLowerCase()];
if (isNotEmpty(token)) {
this.saveXsrfToken(token);
}
const responsePath = JSON.parse(response); const responsePath = JSON.parse(response);
this.onCompleteItem.emit(responsePath); this.onCompleteItem.emit(responsePath);
} }