Angular 2 AuthGuard + Firebase Auth

前端 未结 2 1963
暖寄归人
暖寄归人 2020-12-13 15:02

I\'m trying to build an AuthGuard for Angular 2 routes using Firebase Auth.

This is the AuthGuard Service:

import { Injectable }             from \'@         


        
2条回答
  •  盖世英雄少女心
    2020-12-13 15:50

    Complete the observable using .take(1) to make the component render.

    import {Observable} from "rxjs/Observable";
    import 'rxjs/add/operator/map';
    import 'rxjs/add/operator/take';
    
    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        return this.af.auth.map((auth) => {
            if (!auth) {
              this.router.navigateByUrl('login');
              return false;
            }
            return true;
        }).take(1);
      }
    

提交回复
热议问题