import { Injectable } from '@angular/core' ; import { CanActivate , RouterStateSnapshot , Router , ActivatedRouteSnapshot } from "@angular/router" ; import { AuthService } from "../api-services/auth.service" ; import { Observable } from "rxjs" ; @ Injectable ( ) export class AdminGuardService implements CanActivate { constructor ( private router , private authService ) { } failedAuthCheck ( route , state ) { this . authService . redirectUrl = state . url ; this . router . navigate ( [ '/greeting/log-in' ] ) ; return Observable . create ( obs => obs . next ( false ) ) ; } canActivate ( route , state ) { if ( ! this . authService . authLoaded ) { return this . authService . loadAndCheckAdmin ( ) . do ( success => { if ( ! success ) { this . failedAuthCheck ( route , state ) ; } } ) ; } else if ( this . authService . isAuthenticated . getValue ( ) && this . authService . isAdmin ) { return Observable . create ( obs => obs . next ( true ) ) ; } else { return this . failedAuthCheck ( route , state ) ; } } }