|
|
@@ -10,6 +10,8 @@ import { Injectable, Injector } from '@angular/core';
|
|
|
import { Router } from '@angular/router';
|
|
|
import { Observable } from 'rxjs';
|
|
|
import { tap } from 'rxjs/operators';
|
|
|
+import { DEFAULT_AUTH_TOKEN_HEADER_KEY } from 'shared/shared-constants';
|
|
|
+import { AdminUiConfig } from 'shared/shared-types';
|
|
|
|
|
|
import { getAppConfig } from '../../app.config';
|
|
|
import { AuthService } from '../../core/providers/auth/auth.service';
|
|
|
@@ -27,13 +29,19 @@ export const AUTH_REDIRECT_PARAM = 'redirectTo';
|
|
|
*/
|
|
|
@Injectable()
|
|
|
export class DefaultInterceptor implements HttpInterceptor {
|
|
|
+ private readonly tokenMethod: AdminUiConfig['tokenMethod'] = 'cookie';
|
|
|
+ private readonly authTokenHeaderKey: string;
|
|
|
+
|
|
|
constructor(
|
|
|
private dataService: DataService,
|
|
|
private injector: Injector,
|
|
|
private authService: AuthService,
|
|
|
private router: Router,
|
|
|
private localStorageService: LocalStorageService,
|
|
|
- ) {}
|
|
|
+ ) {
|
|
|
+ this.tokenMethod = getAppConfig().tokenMethod;
|
|
|
+ this.authTokenHeaderKey = getAppConfig().authTokenHeaderKey || DEFAULT_AUTH_TOKEN_HEADER_KEY;
|
|
|
+ }
|
|
|
|
|
|
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
|
|
this.dataService.client.startRequest().subscribe();
|
|
|
@@ -41,6 +49,7 @@ export class DefaultInterceptor implements HttpInterceptor {
|
|
|
tap(
|
|
|
event => {
|
|
|
if (event instanceof HttpResponse) {
|
|
|
+ this.checkForAuthToken(event);
|
|
|
this.notifyOnError(event);
|
|
|
this.dataService.client.completeRequest().subscribe();
|
|
|
}
|
|
|
@@ -105,4 +114,17 @@ export class DefaultInterceptor implements HttpInterceptor {
|
|
|
const notificationService = this.injector.get<NotificationService>(NotificationService);
|
|
|
notificationService.error(message, vars);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * If the server is configured to use the "bearer" tokenMethod, each response should be checked
|
|
|
+ * for the existence of an auth token.
|
|
|
+ */
|
|
|
+ private checkForAuthToken(response: HttpResponse<any>) {
|
|
|
+ if (this.tokenMethod === 'bearer') {
|
|
|
+ const authToken = response.headers.get(this.authTokenHeaderKey);
|
|
|
+ if (authToken) {
|
|
|
+ this.localStorageService.set('authToken', authToken);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|