src/app/services/auth.interceptor.ts
Properties |
|
Methods |
constructor(auth: AuthService, logger: LogService)
|
|||||||||
Defined in src/app/services/auth.interceptor.ts:18
|
|||||||||
Parameters :
|
intercept | |||||||||
intercept(request: HttpRequest
|
|||||||||
Defined in src/app/services/auth.interceptor.ts:20
|
|||||||||
Parameters :
Returns :
Observable<HttpEvent<any>>
|
Public auth |
auth:
|
Type : AuthService
|
Defined in src/app/services/auth.interceptor.ts:19
|
import 'rxjs/add/operator/do';
import {
HttpErrorResponse,
HttpEvent,
HttpHandler,
HttpInterceptor,
HttpRequest,
HttpResponse
} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { AuthService } from './auth.service';
import { LogService } from './log.service';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(public auth: AuthService, private logger: LogService) {}
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${this.auth.getToken()}`
}
});
return next.handle(request).do(
// (event: HttpEvent<any>) => {
// if (event instanceof HttpResponse) {
// this.logger.log(event);
// }
// },
(err: any) => {
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
this.logger.log(err);
this.auth.logout();
}
}
}
);
}
}