2019-12-15 13:49:35 +00:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import {
|
|
|
|
CanActivate,
|
|
|
|
ActivatedRouteSnapshot,
|
|
|
|
RouterStateSnapshot,
|
|
|
|
UrlTree,
|
|
|
|
Router
|
|
|
|
} from '@angular/router';
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
|
2019-12-16 00:58:01 +00:00
|
|
|
import {
|
|
|
|
LocalStorageService,
|
|
|
|
SessionStorageService
|
|
|
|
} from '@ucap-webmessenger/web-storage';
|
2019-12-15 13:49:35 +00:00
|
|
|
import { AppUserInfo, KEY_APP_USER_INFO } from '@app/types/app-user-info.type';
|
|
|
|
import { environment } from '../../environments/environment';
|
|
|
|
import * as AuthenticationStore from '@app/store/account/authentication';
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class AppAutoLoginGuard implements CanActivate {
|
|
|
|
constructor(
|
|
|
|
private router: Router,
|
|
|
|
private store: Store<any>,
|
2019-12-16 00:58:01 +00:00
|
|
|
private localStorageService: LocalStorageService,
|
|
|
|
private sessionStorageService: SessionStorageService
|
2019-12-15 13:49:35 +00:00
|
|
|
) {}
|
|
|
|
|
|
|
|
canActivate(
|
|
|
|
route: ActivatedRouteSnapshot,
|
|
|
|
state: RouterStateSnapshot
|
|
|
|
):
|
|
|
|
| boolean
|
|
|
|
| UrlTree
|
|
|
|
| Observable<boolean | UrlTree>
|
|
|
|
| Promise<boolean | UrlTree> {
|
|
|
|
return new Promise<boolean | UrlTree>((resolve, reject) => {
|
|
|
|
const appUserInfo = this.localStorageService.encGet<AppUserInfo>(
|
|
|
|
KEY_APP_USER_INFO,
|
|
|
|
environment.customConfig.appKey
|
|
|
|
);
|
|
|
|
|
2019-12-16 00:58:01 +00:00
|
|
|
const personLogout: boolean = this.sessionStorageService.get(
|
|
|
|
'PERSON_LOGOUT'
|
|
|
|
);
|
|
|
|
|
2019-12-17 07:29:04 +00:00
|
|
|
if (
|
|
|
|
!!appUserInfo &&
|
|
|
|
appUserInfo.settings.general.autoLogin &&
|
|
|
|
!personLogout
|
|
|
|
) {
|
2019-12-15 13:49:35 +00:00
|
|
|
this.store.dispatch(
|
|
|
|
AuthenticationStore.webLogin({
|
|
|
|
loginInfo: {
|
|
|
|
companyCode: appUserInfo.companyCode,
|
|
|
|
companyGroupType: appUserInfo.companyGroupType,
|
|
|
|
loginId: appUserInfo.loginId,
|
|
|
|
loginPw: appUserInfo.loginPw
|
|
|
|
},
|
|
|
|
rememberMe: appUserInfo.rememberMe,
|
2019-12-17 07:29:04 +00:00
|
|
|
autoLogin: true
|
2019-12-15 13:49:35 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
resolve(false);
|
|
|
|
} else {
|
|
|
|
resolve(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|