import { Authentication } from './../../models/authentication'; import { environment } from './../../environments/environment'; import { AppConstants } from './../../common/AppConstants'; import { Injectable, Input, Output, EventEmitter } from '@angular/core'; import { AppStorageService } from './../common/app-storage.service'; import { SessionStorageService } from './../common/session-storage.service'; import * as _ from 'lodash'; import { JwtHelper } from 'angular2-jwt'; @Injectable() export class UserService { private accessToken: string; public authentication: Authentication; private jwtHelper: JwtHelper; private isTest: boolean; @Output() authenticated: EventEmitter = new EventEmitter(); constructor(private appStorage: AppStorageService, private sessionStorageService: SessionStorageService, private appStorageService: AppStorageService) { this.jwtHelper = new JwtHelper(); this.isTest = environment.envName === 'test' ? true : false; } public get isEulaAccepted() { let accepted: boolean = _.isObject(this.authentication) && !_.isNull(this.authentication.eulaAcceptance) && !_.isEmpty(this.authentication.eulaAcceptance) && this.authentication.eulaAcceptance === this.authentication.userId; return accepted; } public get isAuthenticated() { let isAuthenticated: boolean = _.isObject(this.authentication) && !_.isNull(this.authentication.accessToken) && !_.isEmpty(this.authentication.accessToken); if (isAuthenticated && !this.isTest) { isAuthenticated = !this.jwtHelper.isTokenExpired(this.authentication.accessToken); } return isAuthenticated; } public setAuthenticated(data: any, remember?: boolean) { console.log('Set Authenticated, Data = ', data['userId'], data['userName'], data['company']); this.authentication = { accessToken: data.accessToken, license: data['license'], userId: data['userId'], userName: data['userName'], company: data['company'], eulaAcceptance: this.appStorageService.get(AppConstants.ISB_EULA) }; // save to session this.sessionStorageService.set(AppConstants.authenticationStorageKey, this.authentication); if (remember) { this.appStorage.set(AppConstants.authenticationStorageKey, this.authentication); } else { this.appStorage.remove(AppConstants.authenticationStorageKey); } this.authenticated.emit(this.authentication); } public invalidateAuthentication() { this.authentication = null; this.sessionStorageService.remove(AppConstants.authenticationStorageKey); this.appStorage.remove(AppConstants.authenticationStorageKey); this.appStorage.remove(AppConstants.ISB_EULA); } public removeOptions() { this.sessionStorageService.remove(AppConstants.optionListStorageKey); this.appStorage.remove(AppConstants.optionListStorageKey); } public authenticatedFromStorage() { this.authentication = this.sessionStorageService.get(AppConstants.authenticationStorageKey) || this.appStorage.get(AppConstants.authenticationStorageKey); if (!this.isAuthenticated) { this.invalidateAuthentication(); } else { // trigger authenticated event this.authenticated.emit(this.authentication); } } public onAuthenticated(fn: (authentication: Authentication) => void) { this.authenticated.subscribe((authentication) => { fn; }); } }