1

I have seen and tried a lot of other threads describing the same issue, but couldn't find the solution. I am a beginner in angular technology. I am using angular 11. I am trying to refresh the JWT token in the HttpInterceptor class

this.loginService.checkRefreshToken(this.refreshtoke).pipe(
switchMap((tokenResponse: any) => {
          if (tokenResponse) {
            localStorage.setItem('authToken', tokenResponse.Token)
            localStorage.setItem('refreshToken', tokenResponse.RefreshToken)
            this.tokenSubject.next(tokenResponse);
            console.log('token refreshed');
            return next.handle(this.attachAuthToken(request));
          }
          else return <any>this.loginService.LogOutUser();
        })
      );

The above is my interceptor class calling login service

checkRefreshToken(data : Irefreshtoken) : Observable<any> {
        var body = JSON.stringify(data)
    const httpOptions = {
      headers: new HttpHeaders({
          'Content-Type': 'application/json',
          'Authorization': 'my-auth-token'
      }),
        
    };

    const postURL = this.constants.API_ENDPOINT + this.constants.CheckRefreshToken;

    return this.http.post<any>(postURL,data,httpOptions)
        .pipe(
            tap((result: any) => {
            localStorage.setItem('authToken', result.Token)
            localStorage.setItem('refreshToken', result.RefreshToken)
        })
    );

This is my login service function

[HttpPost]
        [Route("CheckRefreshToken")]
        public async Task<IActionResult> CheckRefreshToken(RefreshToken data)

This is my controller method

The post call is not hitting the back end and is always ending up with error

ERROR TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
    at subscribeTo (subscribeTo.js:27)
    at innerSubscribe (innerSubscribe.js:69)
    at CatchSubscriber.error (catchError.js:36)
    at XMLHttpRequest.onLoad (http.js:1700)
    at ZoneDelegate.invokeTask (zone-evergreen.js:402)
    at Object.onInvokeTask (core.js:28499)
    at ZoneDelegate.invokeTask (zone-evergreen.js:401)
    at Zone.runTask (zone-evergreen.js:174)
    at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:483)
    at invokeTask (zone-evergreen.js:1596)

I know that this is a general error in angular. Any help will be much appreciated. Correct me if I am wrong somewhere. These codes were copied from tutorial codes

Thanks for your help

MITHUN VS
  • 65
  • 7

1 Answers1

0

You can remove the pipe in you checkRefreshToken and just return this.http.post<any>(postURL,data,httpOptions). The logic to save in the local storage is already done in the interceptor.