0

I'm trying to build a route guard class to only allow admin users to visit a specific page. Here is my admin.guard.ts

import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { UserService } from '../services/user.service';

@Injectable()
export class AdminGuard implements CanActivate{
    constructor(
        private router:Router,
        private userService:UserService,
    ){

    }

    canActivate():boolean{
        return this.userService.currentUserIsAdmin();
    }
}

And here is my user service that is being called by admin.guard.ts (im just hard coding a true or false at this stage until I get the guard working).

import { Injectable } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
import { Observable } from 'rxjs';
import { MyAppUser } from '../models/MyAppUser';

@Injectable()
export class UserService {

  users:FirebaseListObservable<any[]>;
  user:FirebaseObjectObservable<any>;
  currentUserIsAdmin:boolean;

  constructor(
    public af:AngularFireDatabase
  ) { 
    this.users = this.af.list('/users') as FirebaseListObservable<MyAppUser[]>; 
    this.currentUserIsAdmin = true;
  }

  getUsers(){
    return this.users;
  }

  isCurrentUserAdmin():boolean{
    return this.currentUserIsAdmin;
  }
}

issue I'm having is the following error

ERROR in C:/projects/xyz/src/app/guards/admin.guard.ts (16,24): 
Cannot invoke an expression whose type lacks a call signature. 
Type 'Boolean' has no compatible call signatures.
Dave
  • 3
  • 2

1 Answers1

0

You are calling your boolean property currentUserIsAdmin as a method. you want to call the actual method instead isCurrentUserAdmin

canActivate():boolean{
    return this.userService.currentUserIsAdmin();
}

should become

canActivate():boolean{
    return this.userService.isCurrentUserAdmin();
}
LLai
  • 13,128
  • 3
  • 41
  • 45