Skip to content

Instantly share code, notes, and snippets.

@lbroudoux
Last active March 13, 2020 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbroudoux/b6068aec8940cbd7ab22194451f1b579 to your computer and use it in GitHub Desktop.
Save lbroudoux/b6068aec8940cbd7ab22194451f1b579 to your computer and use it in GitHub Desktop.
secured-fruits-catalog-k8s-auth.http-interceptors.ts #keycloak
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