import { Injectable } from '@angular/core'; import { Router } from '@angular/router'; import { HttpErrorResponse } from '@angular/common/http'; import { CookieService } from 'ngx-cookie-service'; import { BusService, BaseLocalStorageService } from '@creedinteractive/onguard-ng-services'; import { LaunchpadWebConfiguration } from '@creedinteractive/onguard-models'; import { LaunchpadWebConfigurationService } from '../../launchpad-web-configuration.service'; import { LaunchpadEventsService } from '../launchpad-events/launchpad-events.service'; import { LaunchpadLocalStorageService } from '../launchpad-local-storage/launchpad-local-storage.service'; @Injectable({ providedIn: 'root', }) export class AuthenticationService { private _blockCookie = false; private _jwt: string; private config: LaunchpadWebConfiguration; public redirectUrl: string; public userPermission: string = this.localStorage.user.identity ? this.localStorage.user.identity.permission : 'GENERAL'; private inventoryAssetJwtName = 'jwt-inventory-asset'; constructor( private bus: BusService, private events: LaunchpadEventsService, private router: Router, private localStorage: LaunchpadLocalStorageService, private cookieService: CookieService, private launchpadService: LaunchpadWebConfigurationService, ) { if (typeof launchpadService?.config === 'undefined') { throw new Error('Missing configuration options'); } this.config = launchpadService.config; bus.subscribe(events.user.authentication.success, this.receivedAuthentication, this); bus.subscribe(events.user.authentication.error, this.receivedLogout, this); bus.subscribe(events.user.logout, this.receivedLogout, this); bus.subscribe(events.user.data.success, this.receivedUserData, this); } public get jwt(): string { if (this._jwt) { return this._jwt; } if (this._blockCookie) { return undefined; } const jwtFromCookie = this.cookieService.get(this.inventoryAssetJwtName); return jwtFromCookie ? jwtFromCookie : undefined; } private receivedAuthentication(data) { console.log(`Authentication Service. Authorize with jwt ${data.token}`); console.log(data); this.localStorage.user.locations = data.customers; this.localStorage.user.location = data.customers[0]; this._blockCookie = false; this._jwt = data.token; this.setCookie(); this.redirect(); } private receivedUserData(data) { console.log(`Authentication Service. Received user data.`, data); console.log('this blockCookie: ', this._blockCookie); this.localStorage.user.identity = data; this.userPermission = data.permission; } private setCookie() { this.cookieService.set(this.inventoryAssetJwtName, this.jwt, 365, null, null, null, null); } public blockCookie() { this._blockCookie = true; } private redirect(): void { console.log(`Authentication Service. Redirect after login to ${this.redirectUrl}`); console.log('this blockCookie: ', this._blockCookie); if (this.redirectUrl && !this.redirectUrl.startsWith('/login')) { this.router.navigate([this.redirectUrl]); this.redirectUrl = undefined; } else { this.router.navigate(['']); } } private receivedLogout(error?: HttpErrorResponse): void { console.log(`Authentication Service. Logout. Remove jwt ${this._jwt}`); this._jwt = null; this.blockCookie(); this.cookieService.delete(this.inventoryAssetJwtName); this.localStorage.all.remove(); if (error) { window.location.href = this.config.ssoUrl + `?appLogout=` + this.config.appSsoIdentity; } else { window.location.href = this.config.ssoUrl + `?closelaunchpadtab=true`; } } }