source: Farmatiko/ClientApp/src/app/shared/interceptors/unauthorized.interceptor.ts@ 1db5673

Last change on this file since 1db5673 was 1f4846d, checked in by Mile Jankuloski <mile.jankuloski@…>, 3 years ago

Jwt token auth interceptors, services and guards

  • Property mode set to 100644
File size: 1.1 KB
Line 
1import { Injectable } from '@angular/core';
2import {
3 HttpRequest,
4 HttpHandler,
5 HttpEvent,
6 HttpInterceptor,
7} from '@angular/common/http';
8import { Observable, throwError } from 'rxjs';
9import { catchError } from 'rxjs/operators';
10import { AuthService } from '../services/auth.service';
11import { environment } from '../../../environments/environment';
12import { Router } from '@angular/router';
13
14@Injectable()
15export class UnauthorizedInterceptor implements HttpInterceptor {
16 constructor(private authService: AuthService, private router: Router) {}
17
18 intercept(
19 request: HttpRequest<unknown>,
20 next: HttpHandler
21 ): Observable<HttpEvent<unknown>> {
22 return next.handle(request).pipe(
23 catchError((err) => {
24 if (err.status === 401) {
25 this.authService.clearLocalStorage();
26 this.router.navigate(['login'], {
27 queryParams: { returnUrl: this.router.routerState.snapshot.url },
28 });
29 }
30
31 if (!environment.production) {
32 console.error(err);
33 }
34 const error = (err && err.error && err.error.message) || err.statusText;
35 return throwError(error);
36 })
37 );
38 }
39}
Note: See TracBrowser for help on using the repository browser.