44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import {Injectable} from '@angular/core';
|
|
import {ActivatedRouteSnapshot, CanActivate, CanLoad, Route, Router, RouterStateSnapshot} from '@angular/router';
|
|
import {Observable} from 'rxjs/Observable';
|
|
import {Session} from './utils/helper.class';
|
|
import {LoginErrorCodes} from './utils/guardHelper.class';
|
|
import {map, tap} from "rxjs/operators";
|
|
import {UserManagementService} from "../services/user-management.service";
|
|
|
|
@Injectable()
|
|
export class LoginGuard implements CanActivate, CanLoad {
|
|
|
|
constructor(private router: Router,
|
|
private userManagementService: UserManagementService) {
|
|
}
|
|
|
|
check(path: string): Observable<boolean> | boolean {
|
|
if (Session.isLoggedIn()) {
|
|
return this.userManagementService.getUserInfo(false).pipe(map(user => {
|
|
return user !== null;
|
|
}),tap(isLoggedIn => {
|
|
if(!isLoggedIn) {
|
|
this.router.navigate(['/user-info'], {
|
|
queryParams: {
|
|
'errorCode': LoginErrorCodes.NOT_LOGIN,
|
|
'redirectUrl': path
|
|
}
|
|
});
|
|
}
|
|
}));
|
|
} else {
|
|
this.router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_LOGIN, 'redirectUrl':path}});
|
|
return false;
|
|
}
|
|
}
|
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
|
return this.check(state.url);
|
|
}
|
|
|
|
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
|
|
return this.check('/' + route.path);
|
|
}
|
|
}
|