import { Injectable } from '@angular/core'; import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanLoad, Route} from '@angular/router'; import {Observable} from 'rxjs/Observable'; import {Session} from './utils/helper.class'; import {LoginErrorCodes} from './utils/guardHelper.class'; @Injectable() export class AdminLoginGuard implements CanActivate, CanLoad { constructor(private router: Router) {} check(route: Route) : boolean{ let loggedIn = false; let isAdmin = false; let errorCode = LoginErrorCodes.NOT_LOGIN; if(Session.isLoggedIn()){ loggedIn = true; isAdmin = Session.isPortalAdministrator(); if(!isAdmin){ errorCode = LoginErrorCodes.NOT_ADMIN; } } if(!loggedIn || !isAdmin) { // this.guardHelper.redirect("/user-info",errorCode,state.url); route.path = '/' + route.path; this.router.navigate(['user-info'], {queryParams: {"errorCode": errorCode, "redirectUrl": route.path}}); } return loggedIn && isAdmin; } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { return this.check(route.routeConfig); } canLoad(route: Route): Observable | Promise | boolean { return this.check(route); } }