src/app/auth/auth-interceptor.ts
This interceptor is to transform the HTTP headers by setting the autorization token for all Auth related requests.
Methods |
constructor(authService: AuthService)
|
||||||||
Defined in src/app/auth/auth-interceptor.ts:9
|
||||||||
This is for binding the
Parameters :
|
intercept | ||||||||||||
intercept(req: HttpRequest
|
||||||||||||
Defined in src/app/auth/auth-interceptor.ts:23
|
||||||||||||
Get the Token from
Parameters :
Returns :
any
Call the next method in chain and hand over the transformed request. |
import { HttpHandler, HttpInterceptor, HttpRequest } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { AuthService } from "./auth.service";
/**
* This interceptor is to transform the HTTP headers by setting the autorization token for all Auth related requests.
*/
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
/**
* This is for binding the `AuthService` to this component.
* @param authService holds the AuthService
*/
constructor(private authService: AuthService) {}
/**
* Get the Token from `AuthService` by calling `getToken()` and add the token to the HTTP headers.
* @param req Request to wich the token should be attached.
* @param next Next method in chain.
* @returns Call the next method in chain and hand over the transformed request.
*/
intercept(req: HttpRequest<any>, next: HttpHandler) {
const authToken = this.authService.getToken();
const authRequest = req.clone({
headers: req.headers.set("Authorization", "Bearer " + authToken)
});
return next.handle(authRequest);
}
}