ucap-lg-web/src/app/services/app-authentication.service.ts
richard-loafle 68099247c1 bug fixed
2020-04-07 14:43:00 +09:00

175 lines
4.3 KiB
TypeScript

import { Injectable, Inject } from '@angular/core';
import { LocaleCode } from '@ucap/core';
import { PasswordUtil } from '@ucap/pi';
import {
SessionStorageService,
LocalStorageService
} from '@ucap/ng-web-storage';
import { NativeService } from '@ucap/native';
import { UCAP_NATIVE_SERVICE } from '@ucap/ng-native';
import { LoginSession } from '@app/models/login-session';
import { UserStore } from '@app/models/user-store';
import { AppKey } from '@app/types/app-key.type';
import { environment } from '@environments';
@Injectable({
providedIn: 'root'
})
export class AppAuthenticationService {
constructor(
private sessionStorageService: SessionStorageService,
private localStorageService: LocalStorageService,
@Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService
) {}
get userStore(): UserStore {
const userStore = this.localStorageService.encGet<UserStore>(
AppKey.UserStore,
environment.productConfig.localEncriptionKey
);
return userStore;
}
set userStore(userStore: UserStore) {
const oldUserStore = this.userStore;
this.localStorageService.encSet<UserStore>(
AppKey.UserStore,
{
...oldUserStore,
...userStore
},
environment.productConfig.localEncriptionKey
);
}
get loginSession(): LoginSession {
const loginSession = this.sessionStorageService.get<LoginSession>(
AppKey.LoginSession
);
return loginSession;
}
set loginSession(loginSession: LoginSession) {
const oldLoginSession = this.loginSession;
this.sessionStorageService.set<LoginSession>(AppKey.LoginSession, {
...oldLoginSession,
...loginSession
});
}
loggedIn(): boolean {
const loginSession = this.loginSession;
return !!loginSession && !!loginSession.loginId;
}
useAutoLogin(): UserStore | null {
const userStore = this.userStore;
return !!userStore &&
!!userStore.settings &&
!!userStore.settings.general &&
userStore.settings.general.autoLogin
? userStore
: null;
}
async postWebLogin(
loginSession: LoginSession,
rememberMe: boolean,
autoLogin: boolean
) {
const prevLoginSession = this.loginSession;
const newLoginSession = {
...prevLoginSession,
...loginSession,
localeCode: LocaleCode.Korean
};
const encLoginPw = PasswordUtil.encrypt(newLoginSession.loginPw);
this.loginSession = {
...newLoginSession,
initPw: newLoginSession.loginId === newLoginSession.loginPw,
loginPw: encLoginPw
};
let userStore = this.userStore;
if (!userStore) {
userStore = {
idleCheckTime: 10,
settings: {
...environment.productConfig.defaultSettings,
chat: {
...environment.productConfig.defaultSettings.chat,
downloadPath: await this.nativeService.getPath(
'documents',
environment.productConfig.file.defaultDownloadFolder
)
}
}
};
if (!!environment.productConfig.defaultSettings.general.autoLaunch) {
this.nativeService.changeAutoLaunch(
environment.productConfig.defaultSettings.general.autoLaunch
);
}
}
userStore = {
...userStore,
companyGroupType: loginSession.companyGroupType,
companyCode: loginSession.companyCode,
loginId: loginSession.loginId,
loginPw: loginSession.loginPw
};
if (rememberMe || autoLogin) {
userStore = {
...userStore,
rememberMe
};
userStore.settings.general.autoLogin = autoLogin;
}
this.userStore = userStore;
this.sessionStorageService.remove(AppKey.LogoutSession);
}
logout() {
this.sessionStorageService.remove(AppKey.LoginResponse);
this.sessionStorageService.remove(AppKey.VerInfoResponse);
this.sessionStorageService.remove(AppKey.LoginSession);
this.sessionStorageService.remove(AppKey.UrlInfoResponse);
this.sessionStorageService.remove(AppKey.AuthResponse);
let userStore = this.userStore;
if (!!userStore) {
userStore = {
...userStore,
settings: {
...userStore.settings,
general: {
...userStore.settings.general,
autoLogin: false
}
}
};
this.userStore = userStore;
}
}
}