Broker System for Supercredit
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

46 lines
1.3KB

  1. import {Injectable} from '@angular/core';
  2. import {HttpEvent, HttpEventType, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from '@angular/common/http';
  3. import {Observable} from 'rxjs';
  4. import {tap} from 'rxjs/operators';
  5. import {SessionService} from '../service/session.service';
  6. @Injectable()
  7. export class AuthHttpInterceptor implements HttpInterceptor {
  8. constructor(private ss: SessionService) {
  9. }
  10. intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  11. let h = req.headers;
  12. if (this.ss.loggedIn.hasValidSession()) {
  13. h = h.set('Biukop-Session', this.ss.loggedIn.session);
  14. }
  15. if (this.ss.loggedIn.hasValidMachineId()) {
  16. h = h.set('Biukop-Mid', this.ss.loggedIn.machineId);
  17. }
  18. const authReq = req.clone({
  19. headers: h,
  20. withCredentials: true
  21. });
  22. return next.handle(authReq).pipe(
  23. tap(event => {
  24. // console.log(event);
  25. if (event.type === HttpEventType.Response){
  26. const bs = event.headers.get('biukop-session');
  27. if (bs !== undefined){
  28. if ( this.ss.loggedIn.session !== bs ){
  29. this.ss.loggedIn.session = bs;
  30. this.ss.saveSessionInfo();
  31. console.log('switch session:' , bs);
  32. }
  33. }
  34. }
  35. })
  36. );
  37. }
  38. }