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 } 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, private localStorageService: LocalStorageService ) {} canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): | boolean | UrlTree | Observable | Promise { return new Promise((resolve, reject) => { const appUserInfo = this.localStorageService.encGet( KEY_APP_USER_INFO, environment.customConfig.appKey ); if (!!appUserInfo && appUserInfo.autoLogin) { 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); } }); } }