0

I currently implementing angular4 example application. I am having some problem with auth guard. I want to block route based on the user's role. Firebase - user document

auth.guard.ts

    canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | boolean {

        return this.afAuth.authState
            .take(1)
            .map(user => _.has(_.get(user,'admin'), true))
            .do(authorized => {
                if (!authorized) {
                    console.log('access denied')
                    this.router.navigate(['/login']);

auth.service.ts

  interface User {
  uid: string;
  email: string;
  photoURL?: string;
  displayName?: string;
  admin?: boolean;
}


  user: Observable<User>;


  constructor(private afAuth: AngularFireAuth,
              private afs: AngularFirestore,
              private router: Router) {


                this.user = this.afAuth.authState
                  .switchMap(user => {
                    if (user) {
                      return this.afs.doc<User>
                   (`users/${user.uid}`).valueChanges();
                    } else {

                      return Observable.of(null);
                    }
                  });
               }

I always have 'access denied'. The problem is: the guard never allows to access to administration route.

0 Answers0