2019-09-13 12:02:30 +02:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {ActivatedRouteSnapshot, CanActivate, CanLoad, Route, Router, RouterStateSnapshot} from '@angular/router';
|
2019-09-13 10:26:09 +02:00
|
|
|
import {Observable} from 'rxjs/Observable';
|
2017-12-19 13:53:46 +01:00
|
|
|
import {Session} from './utils/helper.class';
|
2018-11-01 18:20:05 +01:00
|
|
|
import {LoginErrorCodes} from './utils/guardHelper.class';
|
2020-01-29 11:20:53 +01:00
|
|
|
import {filter, map} from "rxjs/operators";
|
|
|
|
import {UserManagementService} from "../services/user-management.service";
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
@Injectable()
|
2019-09-13 10:26:09 +02:00
|
|
|
export class LoginGuard implements CanActivate, CanLoad {
|
2020-01-29 11:20:53 +01:00
|
|
|
|
|
|
|
constructor(private router: Router,
|
|
|
|
private userManagementService: UserManagementService) {
|
2019-02-26 16:25:52 +01:00
|
|
|
}
|
2020-01-29 11:20:53 +01:00
|
|
|
|
|
|
|
check(path: string): Observable<boolean> | boolean {
|
2019-09-13 10:26:09 +02:00
|
|
|
if (Session.isLoggedIn()) {
|
2020-07-02 17:02:14 +02:00
|
|
|
const obs = this.userManagementService.getUserInfo(false).pipe(map(user => {
|
2020-01-29 11:20:53 +01:00
|
|
|
return user !== null;
|
|
|
|
}));
|
|
|
|
obs.pipe(filter(isLoggedIn => !isLoggedIn)).subscribe(() => {
|
|
|
|
this.router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_LOGIN, path}});
|
|
|
|
});
|
|
|
|
return obs;
|
|
|
|
} else {
|
2019-09-13 10:26:09 +02:00
|
|
|
this.router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_LOGIN, path}});
|
2020-01-29 11:20:53 +01:00
|
|
|
return false;
|
2019-09-13 10:26:09 +02:00
|
|
|
}
|
2019-01-25 12:18:38 +01:00
|
|
|
}
|
2020-01-29 11:20:53 +01:00
|
|
|
|
2019-01-25 12:18:38 +01:00
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
2019-01-29 14:03:31 +01:00
|
|
|
return this.check(state.url);
|
2019-01-25 12:18:38 +01:00
|
|
|
}
|
2020-01-29 11:20:53 +01:00
|
|
|
|
2019-09-13 10:26:09 +02:00
|
|
|
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
|
|
|
|
return this.check('/' + route.path);
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|