Last change
on this file since 28d7d35 was 1f4846d, checked in by Mile Jankuloski <mile.jankuloski@…>, 4 years ago |
Jwt token auth interceptors, services and guards
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[1f4846d] | 1 | import { Injectable } from '@angular/core';
|
---|
| 2 | import {
|
---|
| 3 | HttpRequest,
|
---|
| 4 | HttpHandler,
|
---|
| 5 | HttpEvent,
|
---|
| 6 | HttpInterceptor,
|
---|
| 7 | } from '@angular/common/http';
|
---|
| 8 | import { Observable, throwError } from 'rxjs';
|
---|
| 9 | import { catchError } from 'rxjs/operators';
|
---|
| 10 | import { AuthService } from '../services/auth.service';
|
---|
| 11 | import { environment } from '../../../environments/environment';
|
---|
| 12 | import { Router } from '@angular/router';
|
---|
| 13 |
|
---|
| 14 | @Injectable()
|
---|
| 15 | export 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.