@armor/create-armor-ui
Version:
A CLI tool for generating Armor UI apps.
85 lines (72 loc) • 2.86 kB
text/typescript
import { Component } from '@angular/core';
import { LocaleService, NavStateService } from '@armor/brandkit';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
import { NAV_ITEMS } from './app.nav';
import { IdentityService, IMe, LoginService } from '@armor/api';
import { CONFIGURATION_CONTEXT, LocalStorageService, WindowService } from '@armor/platform-browser';
({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
constructor(
private httpClient: HttpClient,
private identityService: IdentityService,
private localeService: LocaleService,
private loginService: LoginService,
private localStorageService: LocalStorageService,
private navStateService: NavStateService,
private router: Router,
private windowService: WindowService
) {
this.window = windowService.getWindowReference();
this.loginService.handleAuthorizationCode(() => {
// Add functions here if you need to globally trigger anything after authorization/identity is processed.
// TODO (DATA-945): Add code to register the fullStory user.
// TODO (DATA-881): Add code to register google analytics and send to GA on route change (see portal).
setTimeout(() => {
const url = this.router.url.replace(new RegExp('[?&]authorization_code=[^&#]*(#.*)?$'), '$1');
window.history.pushState({ authCodeProcessed: true }, window.document.title, url);
});
// TODO (DATA-947): Remove this code once proper idle logout handling is in place.
setInterval(() => {
this.requestReissue();
}, 100000);
this.localeService.init();
// Initialize navigation state service.
this.navState = this.navStateService.state;
this.navStateService.navStateChanged$.subscribe((_state: string) => {
this.onNavStateChanged(_state);
});
});
}
public navItems: any[] = NAV_ITEMS;
public navState: string;
public window: any;
public onNavStateChanged(_state: string) {
this.navState = _state;
}
public settings(): void {
this.identityService.onIdentityReady(async (me: IMe) => {
await this.router.navigateByUrl(`/account/users/${ me.user.id }`);
});
}
public logout(): void {
this.loginService.logout();
}
public requestReissue() {
this.httpClient.post(`${CONFIGURATION_CONTEXT.instance.endpoints.api}auth/token/reissue`,
{ token: this.localStorageService.get('auth-token') },
{
headers: {
Authorization: `FH-AUTH ${ this.localStorageService.get('auth-token') }`,
'Content-Type': 'application/json'
}
}
).subscribe((response: any) => {
this.localStorageService.set('auth-token', response.access_token);
});
}
}