import { Injectable, Inject } 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'; import { Settings } from '@ucap-webmessenger/ui-settings'; import { NativeService, UCAP_NATIVE_SERVICE } from '@ucap-webmessenger/native'; @Injectable({ providedIn: 'root' }) export class AppAuthenticationService { showLoader = false; constructor( private sessionStorageService: SessionStorageService, private localStorageService: LocalStorageService, @Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService, private daesangCipherService: DaesangCipherService ) {} authenticated(): boolean { const loginInfo = this.sessionStorageService.get(KEY_LOGIN_INFO); return null !== loginInfo && !!loginInfo.loginId; } async 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, chat: { ...environment.productConfig.defaultSettings.chat, downloadPath: `${await this.nativeService.getPath( 'documents' )}/LG UCAP downloads` } } }; } appUserInfo = { ...appUserInfo, companyGroupType: loginInfo.companyGroupType, companyCode: loginInfo.companyCode, loginId: loginInfo.loginId, loginPw: loginInfo.loginPw }; if (rememberMe || autoLogin) { appUserInfo = { ...appUserInfo, rememberMe }; appUserInfo.settings.general.autoLogin = 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); } }