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

69 lines
1.9 KiB
TypeScript

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';
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>,
private localStorageService: LocalStorageService,
private sessionStorageService: SessionStorageService
) {}
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: boolean = this.sessionStorageService.get(
'PERSON_LOGOUT'
);
if (!!appUserInfo && appUserInfo.autoLogin && !personLogout) {
this.store.dispatch(
AuthenticationStore.webLogin({
loginInfo: {
companyCode: appUserInfo.companyCode,
companyGroupType: appUserInfo.companyGroupType,
loginId: appUserInfo.loginId,
loginPw: appUserInfo.loginPw
},
rememberMe: appUserInfo.rememberMe,
autoLogin: appUserInfo.autoLogin
})
);
resolve(false);
} else {
resolve(true);
}
});
}
}