Last active
March 13, 2020 12:44
-
-
Save lbroudoux/b6068aec8940cbd7ab22194451f1b579 to your computer and use it in GitHub Desktop.
secured-fruits-catalog-k8s-auth.http-interceptors.ts #keycloak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http'; | |
import { Observable } from 'rxjs'; | |
import { AuthenticationService } from "./auth.service"; | |
@Injectable() | |
export class AuthenticationHttpInterceptor implements HttpInterceptor { | |
constructor(protected authService: AuthenticationService) { } | |
//constructor() { } | |
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
console.log('[AuthenticationHttpInterceptor] intercept for ' + req.method); | |
if (req.method === 'OPTIONS') { | |
return next.handle(req); | |
} | |
// Build new set of headers for authentication purpose. | |
if (this.authService.isAuthenticated) { | |
var authHeaders: {[header: string]: string} = {}; | |
this.authService.injectAuthHeaders(authHeaders); | |
const changedReq = req.clone({setHeaders: authHeaders}); | |
return next.handle(changedReq); | |
} | |
return next.handle(req); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment