Error in observable

33 views
Skip to first unread message

moodi wahadan

unread,
Apr 3, 2024, 1:42:17 AMApr 3
to Angular and AngularJS discussion
Hi,

in the below code:

import { TokenApiModel } from '../model/token.api.model';
import { Router } from '@angular/router';
import { NgToastService } from 'ng-angular-popup';
import { AuthService } from '../services/authService';
import { Injectable } from '@angular/core';
import {
  HttpRequest,
  HttpHandler,
  HttpInterceptor,
  HttpErrorResponse,
  HttpEvent
} from '@angular/common/http';
import { catchError, Observable, switchMap, throwError } from 'rxjs';
import { token } from 'aws-sdk/clients/sns';

@Injectable()
export class TokenInterceptor implements HttpInterceptor {

  constructor(private auth: AuthService, private toast: NgToastService, private router: Router) {}

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const myToken = this.auth.getToken()
    console.log("token is ok...")

    // this.start.load();
    if(myToken){
      request = request.clone({
        setHeaders: {Authorization: "Bearer "+ myToken}
      })
    }

    return next.handle(request).pipe(
      catchError((err)=>{
        if(err instanceof HttpErrorResponse){
          if(err.status === 401){
            //this.toast.warning({detail:"Warning", summary:"Token is expired, Please Login again"});
            //this.router.navigate(['login'])
            //handle
            return this.handleUnAuthorizedError(request,next);
          }
        }
        return throwError(()=> err)
      })
    );
  }

I have error the return statement:
return next.handle(request).pipe(

error:

Type 'Observable<unknown>' is not assignable to type 'Observable<HttpEvent<any>>'.
Type 'unknown' is not assignable to type 'HttpEvent<any>'

how to solve this issue?
Reply all
Reply to author
Forward
0 new messages