32 lines
972 B
TypeScript
32 lines
972 B
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';
|
|
|
|
@Injectable()
|
|
export class LoginGuard implements CanActivate, CanLoad {
|
|
|
|
constructor(private router: Router) {
|
|
}
|
|
|
|
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}});
|
|
}
|
|
return loggedIn;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|