import { Injectable } from '@angular/core'; import { SessionStorageService, LocalStorageService } from '@ucap-webmessenger/web-storage'; import { LocaleCode } from '@ucap-webmessenger/core'; import { LoginInfo, KEY_LOGIN_INFO, KEY_LOGIN_RES_INFO, KEY_VER_INFO, KEY_URL_INFO } from '../types'; import { PasswordUtil } from '@ucap-webmessenger/pi'; import { DaesangCipherService } from '@ucap-webmessenger/daesang'; import { environment } from '../../environments/environment'; import { AppUserInfo, KEY_APP_USER_INFO } from '@app/types/app-user-info.type'; @Injectable({ providedIn: 'root' }) export class AppAuthenticationService { showLoader = false; constructor( private sessionStorageService: SessionStorageService, private localStorageService: LocalStorageService, private daesangCipherService: DaesangCipherService ) {} authenticated(): boolean { const loginInfo = this.sessionStorageService.get(KEY_LOGIN_INFO); return null !== loginInfo && !!loginInfo.loginId; } login(loginInfo: LoginInfo, rememberMe: boolean, autoLogin: boolean) { loginInfo = { ...loginInfo, localeCode: LocaleCode.Korean }; const encLoginPw = this.daesangCipherService.encrypt( environment.customConfig.pw.userKey, loginInfo.loginPw, environment.customConfig.pw.isBase64 ); // PasswordUtil.encrypt(loginInfo.loginPw) this.sessionStorageService.set(KEY_LOGIN_INFO, { ...loginInfo, initPw: loginInfo.loginId === loginInfo.loginPw, loginPw: encLoginPw }); let appUserInfo = this.localStorageService.encGet( KEY_APP_USER_INFO, environment.customConfig.appKey ); if (!appUserInfo) { appUserInfo = { settings: environment.productConfig.defaultSettings }; } if (rememberMe || autoLogin) { appUserInfo = { ...appUserInfo, companyGroupType: loginInfo.companyGroupType, companyCode: loginInfo.companyCode, loginId: loginInfo.loginId, loginPw: autoLogin ? loginInfo.loginPw : undefined, rememberMe, autoLogin }; } this.localStorageService.encSet( KEY_APP_USER_INFO, appUserInfo, environment.customConfig.appKey ); this.sessionStorageService.remove('PERSON_LOGOUT'); } logout() { this.sessionStorageService.remove(KEY_LOGIN_RES_INFO); this.sessionStorageService.remove(KEY_VER_INFO); this.sessionStorageService.remove(KEY_LOGIN_INFO); this.sessionStorageService.remove(KEY_URL_INFO); this.sessionStorageService.set('PERSON_LOGOUT', true); } }