feat: complete todo and utilize getHeaders

pull/5430/head
bnymncoskuner 5 years ago
parent 2f4fb58d02
commit 03364dfc7a

@ -1,5 +1,5 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http'; import { HttpInterceptor, HttpHandler, HttpRequest, HttpHeaders } from '@angular/common/http';
import { OAuthService } from 'angular-oauth2-oidc'; import { OAuthService } from 'angular-oauth2-oidc';
import { Store } from '@ngxs/store'; import { Store } from '@ngxs/store';
import { SessionState } from '../states'; import { SessionState } from '../states';
@ -13,48 +13,30 @@ export class ApiInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler) { intercept(request: HttpRequest<any>, next: HttpHandler) {
this.store.dispatch(new StartLoader(request)); this.store.dispatch(new StartLoader(request));
const headers = {} as any;
// TODO: utilize getHeaders method here WITH GREAT CARE!
const token = this.oAuthService.getAccessToken();
if (!request.headers.has('Authorization') && token) {
headers['Authorization'] = `Bearer ${token}`;
}
const lang = this.store.selectSnapshot(SessionState.getLanguage);
if (!request.headers.has('Accept-Language') && lang) {
headers['Accept-Language'] = lang;
}
const tenant = this.store.selectSnapshot(SessionState.getTenant);
if (!request.headers.has('__tenant') && tenant) {
headers['__tenant'] = tenant.id;
}
return next return next
.handle( .handle(
request.clone({ request.clone({
setHeaders: headers, setHeaders: this.getHeaders(request.headers),
}), }),
) )
.pipe(finalize(() => this.store.dispatch(new StopLoader(request)))); .pipe(finalize(() => this.store.dispatch(new StopLoader(request))));
} }
getHeaders() { getHeaders(existingHeaders?: HttpHeaders) {
const headers = {} as any; const headers = {} as any;
const token = this.oAuthService.getAccessToken(); const token = this.oAuthService.getAccessToken();
if (token) { if (!existingHeaders?.has('Authorization') && token) {
headers['Authorization'] = `Bearer ${token}`; headers['Authorization'] = `Bearer ${token}`;
} }
const lang = this.store.selectSnapshot(SessionState.getLanguage); const lang = this.store.selectSnapshot(SessionState.getLanguage);
if (lang) { if (!existingHeaders?.has('Accept-Language') && lang) {
headers['Accept-Language'] = lang; headers['Accept-Language'] = lang;
} }
const tenant = this.store.selectSnapshot(SessionState.getTenant); const tenant = this.store.selectSnapshot(SessionState.getTenant);
if (tenant) { if (!existingHeaders?.has('__tenant') && tenant) {
headers['__tenant'] = tenant.id; headers['__tenant'] = tenant.id;
} }

Loading…
Cancel
Save