61 lines
1.7 KiB
TypeScript
61 lines
1.7 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 } 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
|
||
|
) {}
|
||
|
|
||
|
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
|
||
|
);
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|