UNPKG

2.78 kBPlain TextView Raw
1import { Component } from '@angular/core';
2import { NavStateService } from '@armor/brandkit';
3import { HttpClient } from '@angular/common/http';
4import { Router } from '@angular/router';
5
6import { NAV_ITEMS } from './app.nav';
7import { LoginService } from '@armor/api';
8import { CONFIGURATION_CONTEXT, LocalStorageService, WindowService } from '@armor/platform-browser';
9
10@Component({
11 selector: 'app-root',
12 templateUrl: './app.component.html',
13 styleUrls: ['./app.component.scss']
14})
15export class AppComponent {
16 constructor(
17 private loginService: LoginService,
18 private navStateService: NavStateService,
19 private windowService: WindowService,
20 private localStorageService: LocalStorageService,
21 private _httpClient: HttpClient,
22 private router: Router
23 ) {
24 this.httpClient = _httpClient;
25
26 // Initialize navigation state service.
27 this.navState = this.navStateService.state;
28 this.navStateService.navStateChanged$.subscribe((_state: string) => {
29 this.onNavStateChanged(_state);
30 });
31
32 this.window = windowService.getWindowReference();
33 this.loginService.handleAuthorizationCode(() => {
34 // Add functions here if you need to globally trigger anything after authorization/identity is processed.
35 // TODO: Add code to register the fullstory user.
36
37 // TODO: remove this redundant pushState call
38 // It is working from the IMC & AMP, but somethign about the current routing on create-armor-ui
39 // is making it so that it needs the extra setTimeout to work correctly
40 setTimeout(() => {
41 const url = this.router.url.replace(new RegExp('[?&]authorization_code=[^&#]*(#.*)?$'), '$1');
42 window.history.pushState({ authCodeProcessed: true }, window.document.title, url);
43 });
44
45 // TODO: Remove this code once proper idle logout handling is in place.
46 setInterval(() => {
47 this.requestReissue();
48 }, 100000);
49 });
50 }
51
52 public navItems: any[] = NAV_ITEMS;
53 public navState: string;
54 public window: any;
55 public httpClient: HttpClient;
56
57 public onNavStateChanged(_state: string) {
58 this.navState = _state;
59 }
60
61 public requestReissue() {
62 // TODO: Remove this code once proper idle logout handling is in place.
63 this.httpClient.post(`${CONFIGURATION_CONTEXT.instance.endpoints.api}auth/token/reissue`,
64 { token: this.localStorageService.get('auth-token') },
65 {
66 headers: {
67 Authorization: `FH-AUTH ${ this.localStorageService.get('auth-token') }`,
68 'Content-Type': 'application/json'
69 }
70 }
71 ).subscribe((response: any) => {
72 console.log(`[AMP] Token reissued. New token is "${ response.access_token }"`);
73 this.localStorageService.set('auth-token', response.access_token);
74 });
75 }
76}