0

我目前正在实现 angular4 示例应用程序。我在使用 auth guard 时遇到了一些问题。我想根据用户的角色阻止路由。 Firebase - 用户文档

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);
                    }
                  });
               }

我总是“拒绝访问”。问题是:守卫永远不允许访问管理路径。

4

0 回答 0