bleenco/ngx-uploader

Using Http Interceptor

Opened this issue · 2 comments

Hi !

I was wondering how to use my Custom HttpInterceptor with ngx-uploader. Since it is not using HttpClient but XMLHttpRequest directly, interception is nerver made. That would be useful in the case some JWT token validation needs to be done, etc.

Is it something plan ?

Best Regards,
Yannick

XMLHttpRequest simply doesn't work well with interceptors, and that is likely why this repo's headers: { key: value } exists when building your upload request.

I use Authorization headers with a localstorage token, which should be standard:

  ngOnInit() {
    const token = localStorage.getItem('token');
    if (token) {
      this.authHeader = { Authorization: `bearer ${token}` };
    } else {
      this.authService.logout();
    }
  
  startUpload(): void {
    const event: UploadInput = {
      type: 'uploadAll',
      url: this.url,
      method: 'POST',
      data: { foo: 'bar' },
      headers: this.authHeader
    };

This feature likely won't be available unless Angular decides to build in support to HttpClientModule for XMLHttpRequest.

Thanks for your answer ! I extracted most of my interceptor logic so I can reuse it while interacting with ngx-uploader