Guest User

Untitled

a guest
Jul 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. import { Injectable } from "@angular/core";
  2. import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from
  3. "@angular/common/http";
  4. import { Observable } from "rxjs";
  5.  
  6. import { config } from "../config";
  7. import { AuthService } from "../shared/services/auth.service";
  8.  
  9. @Injectable()
  10. export class AuthInterceptor implements HttpInterceptor {
  11.  
  12. constructor(private authService: AuthService) { }
  13.  
  14. intercept(req: HttpRequest<any>, next: HttpHandler):
  15. Observable<HttpEvent<any>> {
  16.  
  17. let headers = {
  18. 'Accept': 'application/json',
  19. 'Authorization': `Bearer ${localStorage.getItem('access_token')}`,
  20. 'Ocp-Apim-Subscription-Key': config.subscriptionKey
  21. }
  22.  
  23. if (req.url.includes(`${config.apiBaseUrl}auth/login`)) {
  24.  
  25. headers['Authorization'] = 'Basic ' + btoa(this.authService.username
  26. + ':' + this.authService.password);
  27.  
  28. } else if (req.url.includes(config.notificationApiBaseUrl)) {
  29.  
  30. headers['Ocp-Apim-Subscription-Key'] =
  31. config.notificationSubscriptionKey;
  32. }
  33.  
  34. const modified = req.clone({
  35. setHeaders: headers
  36. });
  37.  
  38. return next.handle(modified);
  39. }
  40. }
Add Comment
Please, Sign In to add comment