src/lib/service/auth/auth-interceptor.ts
Properties |
|
Methods |
constructor(auth: AuthService)
|
||||||
Defined in src/lib/service/auth/auth-interceptor.ts:8
|
||||||
Parameters :
|
intercept | |||||||||
intercept(request: HttpRequest
|
|||||||||
Defined in src/lib/service/auth/auth-interceptor.ts:13
|
|||||||||
Parameters :
Returns :
Observable<HttpEvent<any>>
|
Public auth |
auth:
|
Type : AuthService
|
Defined in src/lib/service/auth/auth-interceptor.ts:10
|
import {Injectable} from '@angular/core';
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
import {Observable} from 'rxjs';
import {tap} from 'rxjs/operators';
import {AuthService} from './auth.service';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(public auth: AuthService) {
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(tap((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
// do stuff with response if you want
}
}, (error: any) => {
if (error instanceof HttpErrorResponse || error.isHttpErrorResponse) {
if (error.status === 401) {
this.auth.logout();
}
}
}));
}
}