45 lines
1.6 KiB
TypeScript
45 lines
1.6 KiB
TypeScript
|
import {Injectable} from '@angular/core';
|
||
|
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
|
||
|
import {Observable} from 'rxjs/Observable';
|
||
|
import {Session} from './utils/helper.class';
|
||
|
import {LoginErrorCodes} from './utils/guardHelper.class';
|
||
|
import {filter, map, mergeMap} from "rxjs/operators";
|
||
|
import {UserManagementService} from "../services/user-management.service";
|
||
|
import {UserRegistryService} from "../services/user-registry.service";
|
||
|
import {of} from "rxjs";
|
||
|
|
||
|
@Injectable()
|
||
|
export class VerificationGuard implements CanActivate {
|
||
|
|
||
|
constructor(private router: Router,
|
||
|
private userRegistryService: UserRegistryService,
|
||
|
private userManagementService: UserManagementService) {
|
||
|
}
|
||
|
|
||
|
check(path: string): Observable<boolean> | boolean {
|
||
|
if (Session.isLoggedIn()) {
|
||
|
const obs = this.userManagementService.getUserInfo(false).pipe(map(user => {
|
||
|
if(user) {
|
||
|
of(true);
|
||
|
} else {
|
||
|
return of(false);
|
||
|
}
|
||
|
}), mergeMap(authorized => {
|
||
|
return authorized;
|
||
|
}));
|
||
|
obs.pipe(filter(isLoggedIn => !isLoggedIn)).subscribe(() => {
|
||
|
this.router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_LOGIN, path}});
|
||
|
});
|
||
|
return obs;
|
||
|
} else {
|
||
|
this.router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_LOGIN, path}});
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
||
|
console.log(route);
|
||
|
return this.check(state.url);
|
||
|
}
|
||
|
}
|