63 lines
1.7 KiB
TypeScript
Raw Normal View History

2019-09-18 15:02:21 +09:00
import { Injectable } from '@angular/core';
import CryptoJS from 'crypto-js';
import {
SessionStorageService,
LocalStorageService
} from '@ucap-webmessenger/web-storage';
2019-09-19 10:40:16 +09:00
import { EnviromentUtilService } from '@ucap-webmessenger/util';
import { DeviceType } from '@ucap-webmessenger/core';
2019-09-19 17:03:39 +09:00
import { LoginInfo, KEY_LOGIN_INFO } from '../types';
2019-09-18 15:02:21 +09:00
@Injectable({
providedIn: 'root'
})
export class AuthenticationService {
showLoader = false;
constructor(
private sessionStorageService: SessionStorageService,
2019-09-19 10:40:16 +09:00
private localStorageService: LocalStorageService,
private enviromentUtilService: EnviromentUtilService
2019-09-18 15:02:21 +09:00
) {}
authenticated(): boolean {
const loginInfo = this.sessionStorageService.get<LoginInfo>(KEY_LOGIN_INFO);
return null !== loginInfo && !!loginInfo.loginId;
}
login(loginInfo: LoginInfo, rememberMe: boolean) {
2019-09-19 10:40:16 +09:00
let deviceType: DeviceType;
if (this.enviromentUtilService.nodeWebkit()) {
deviceType = DeviceType.PC;
} else if (this.enviromentUtilService.android()) {
deviceType = DeviceType.Android;
} else if (this.enviromentUtilService.ios()) {
deviceType = DeviceType.iOS;
} else {
deviceType = DeviceType.Web;
}
2019-09-18 15:02:21 +09:00
this.sessionStorageService.set<LoginInfo>(KEY_LOGIN_INFO, {
...loginInfo,
2019-09-19 10:40:16 +09:00
loginPw: CryptoJS.enc.Hex.stringify(CryptoJS.SHA256(loginInfo.loginPw)),
deviceType
2019-09-18 15:02:21 +09:00
});
if (rememberMe) {
this.localStorageService.set<LoginInfo>(KEY_LOGIN_INFO, {
...loginInfo,
loginPw: undefined
});
} else {
this.localStorageService.remove(KEY_LOGIN_INFO);
}
}
2019-09-19 17:03:39 +09:00
logout() {
this.sessionStorageService.remove(KEY_LOGIN_INFO);
}
2019-09-18 15:02:21 +09:00
}