Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from "@angular/core";
- import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from
- "@angular/common/http";
- import { Observable } from "rxjs";
- import { config } from "../config";
- import { AuthService } from "../shared/services/auth.service";
- @Injectable()
- export class AuthInterceptor implements HttpInterceptor {
- constructor(private authService: AuthService) { }
- intercept(req: HttpRequest<any>, next: HttpHandler):
- Observable<HttpEvent<any>> {
- let headers = {
- 'Accept': 'application/json',
- 'Authorization': `Bearer ${localStorage.getItem('access_token')}`,
- 'Ocp-Apim-Subscription-Key': config.subscriptionKey
- }
- if (req.url.includes(`${config.apiBaseUrl}auth/login`)) {
- headers['Authorization'] = 'Basic ' + btoa(this.authService.username
- + ':' + this.authService.password);
- } else if (req.url.includes(config.notificationApiBaseUrl)) {
- headers['Ocp-Apim-Subscription-Key'] =
- config.notificationSubscriptionKey;
- }
- const modified = req.clone({
- setHeaders: headers
- });
- return next.handle(modified);
- }
- }
Add Comment
Please, Sign In to add comment