角4& Firebase auth后卫

时间:2017-11-10 12:00:34

标签: angular firebase authentication oauth auth-guard

我目前正在实施angular4示例应用程序。我对auth警卫有一些问题。 我想根据用户的角色阻止路由。 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);
                    }
                  });
               }

我总是'拒绝访问'。 问题是:警卫从不允许访问管理路线。

0 个答案:

没有答案