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';
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
@Injectable()
|
2019-09-13 10:26:09 +02:00
|
|
|
export class LoginGuard implements CanActivate, CanLoad {
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2019-09-13 10:26:09 +02:00
|
|
|
constructor(private router: Router) {
|
2019-02-26 16:25:52 +01:00
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2019-09-13 10:26:09 +02:00
|
|
|
check(path: string): boolean {
|
|
|
|
let loggedIn = false;
|
|
|
|
if (Session.isLoggedIn()) {
|
|
|
|
loggedIn = true;
|
|
|
|
}
|
|
|
|
if (!loggedIn) {
|
|
|
|
this.router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_LOGIN, path}});
|
|
|
|
}
|
2019-01-25 12:18:38 +01:00
|
|
|
return loggedIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
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
|
|
|
}
|