intercept()

in angular/src/app/app-interceptors/security.interceptor.ts [16:37]


  intercept(req: HttpRequest<any>, next: HttpHandler) {
    const identity = this.securityService.getIdentity();
    if (identity && identity.token) {
      req = req.clone({ headers: req.headers.set('Authorization', 'Bearer ' + identity.token) });
    }

    return next.handle(req).pipe(
      map(data => {
        return data;
      }),
      catchError(err => {
        if (err instanceof HttpErrorResponse) {
          if (err.status === 401) {
            this.securityService.logout();
            this.router.navigate(['/login']);
          }
        }

        return throwError(err);
      })
    );
  }