next-ucap-messenger/projects/ucap-webmessenger-app/src/app/guards/auto-login.guard.ts

74 lines
2.0 KiB
TypeScript
Raw Normal View History

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';
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';
import { KEY_LOGOUT_INFO, LogoutInfo } from '@app/types';
2019-12-15 13:49:35 +00:00
@Injectable({
providedIn: 'root'
})
export class AppAutoLoginGuard implements CanActivate {
constructor(
private router: Router,
private store: Store<any>,
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
);
const personLogout: LogoutInfo = this.sessionStorageService.get(
KEY_LOGOUT_INFO
);
2019-12-17 07:29:04 +00:00
if (
!!appUserInfo &&
appUserInfo.settings.general.autoLogin &&
2020-01-06 05:49:40 +00:00
!(!!personLogout && !!personLogout.personLogout)
2019-12-17 07:29:04 +00:00
) {
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);
}
});
}
}