Interceptor not intercepting http requests (Angular 6)

前端 未结 5 872
自闭症患者
自闭症患者 2021-01-01 11:10

I\'m in the proces of adding an interceptor to my angular 6 project. To make calls to my API, I need to add a bearer token to all calls. Unfortunately the interceptor does n

5条回答
  •  情书的邮戳
    2021-01-01 11:51

    In my case i had a post method like this:

    this.http.post(this.authUrl, JSON.stringify({username: username, password: password})).pipe(
          map((response: any) => {
                let token: any = response.token;
                if (token) {
                    localStorage.setItem('currentUser', 'value' }));                
                    return response; 
                } else {
                    return null;
                }
            }),
            catchError((error:any) => {return observableThrowError(error.json().error || 'Server error')})
          );
    

    Because the map method could return null the interceptor stopt intercepting the requests made with this post method call. Don't ask me why this is so, but as soon as i replace null with say response everything started working again.

提交回复
热议问题