1 | import { Component } from '@angular/core';
|
2 | import { NavStateService } from '@armor/brandkit';
|
3 | import { HttpClient } from '@angular/common/http';
|
4 | import { Router } from '@angular/router';
|
5 |
|
6 | import { NAV_ITEMS } from './app.nav';
|
7 | import { LoginService } from '@armor/api';
|
8 | import { 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 | })
|
15 | export 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 |
|
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 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
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 |
|
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 |
|
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 | }
|