import { Injectable } from '@angular/core' ; import { Router , CanActivate } from '@angular/router' ; import { AngularFireDatabase } from '@angular/fire/database' ; import { GlobalService } from '../services/global.service' ; import { Observable } from 'rxjs' ; @ Injectable ( ) export class SuperAdminGuard implements CanActivate { currentUser : any ; constructor ( public router , public db , public globalService ) { this . currentUser = globalService . user . getValue ( ) ; } canActivate ( ) { return new Promise ( Resolve => { if ( this . currentUser ) { this . db . list ( '/admins' , ref => ref . orderByChild ( 'email' ) . equalTo ( this . currentUser . email ) ) . valueChanges ( ) . subscribe ( ( u ) => { if ( u [ 0 ] . role === 'super-admin' ) { return Resolve ( true ) ; } else { this . router . navigate ( [ '/admin' ] ) ; return Resolve ( false ) ; } } ) ; } } ) ; } }