【问题标题】:Using canActivate Angular with AngularFirebase2将 canActivate Angular 与 AngularFirebase2 一起使用
【发布时间】:2019-07-12 08:34:27
【问题描述】:

我已经阅读并尝试过一万亿条建议。

大约 2 年前,我使用 AngularFire2 在 Angular 中进行了 Auth Guard,如下所示:

 canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    this.af.auth.subscribe((auth) =>  {
      if(auth == null) {
        this.router.navigate(['/login']);
        this.allowed = false;
      } else {
        this.allowed = true;
      }
    })
    return this.allowed;
  }

今天,我正在尝试使用今天的 javascript 复制相同的身份验证保护

Angular CLI: 7.3.2
Node: 11.10.0
OS: linux x64
Angular: 7.2.5
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router, service-worker

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.13.2
@angular-devkit/build-angular     0.13.2
@angular-devkit/build-optimizer   0.13.2
@angular-devkit/build-webpack     0.13.2
@angular-devkit/core              7.3.2
@angular-devkit/schematics        7.3.2
@angular/cli                      7.3.2
@angular/fire                     5.1.1
@angular/pwa                      0.12.4
@ngtools/webpack                  7.3.2
@schematics/angular               7.3.2
@schematics/update                0.13.2
rxjs                              6.3.3
typescript                        3.2.4
webpack                           4.29.0

我知道rxjs 发生了一些变化,所以是这样的

this.af.auth.subscribe((auth) => {

现在变成了

this.af.authState.pipe(map(auth) => {

除非执行上述操作会导致浏览器选项卡挂在我身上。

我已经看到了至少 15 个关于如何使用 AngularFirebase 进行路由保护的不同问题,它们都采用不同的方法。

我知道未来几个月情况会发生变化(很可能)如何做到这一点,但截至 2019 年 2 月 18 日,你如何使用 AngularFire2 进行路由保护?

编辑

这就是有趣/奇怪的地方。我在下面尝试了@sergey-mell 答案。

这是我在路由中应用authGuard 的方式

const routes: Routes = [
  { path: 'auth', loadChildren: './auth/auth.module#AuthModule' },
  {
    path: '',
    component: MenuComponent,
    children: [
      { path: '', component: HomeComponent },
      { path: 'new', component: NewcustomerComponent },
      { path: ':customer_id', component: CustomerdetailComponent }
    ],
    canActivate: [AuthGuard]
  }
];

发生的情况是,如果没有登录,我直接在浏览器中访问localhost:4200/,浏览器就会挂起。

在控制台中,Chrome 通知我访问下面的 URL

https://crbug.com/882238

我点击链接,却被告知我无权查看该错误。困扰我的错误。好的。

那么我有什么遗漏吗?

【问题讨论】:

    标签: angular firebase angularfire2


    【解决方案1】:

    前言:

    1. 如果查看 Angular docs Guard 可以返回一个解析为 boolean 的 Observable。
    2. AngularFire docs 表明我们可以使用 AngularFireAuth.user observable 来检查用户身份验证状态。

    所以,对我来说,你的后卫应该如下所示:

    @Injectable()
    class LetMeInGuard implements CanActivate {
      constructor(private afAuth: AngularFireAuth,
                  private routes: Router) {}
    
      canActivate(route: ActivatedRouteSnapshot, 
                  state: RouterStateSnapshot): Observable<boolean> {
        return this.afAuth.user.pipe(
          map(user => {
             if (!user) {
                 this.router.navigate(['/login']);
             }
             return !!user;
          })
        );
      }
    }
    

    【讨论】:

    • 谢谢。使用上述,虽然我有import 'rxjs/operators/map; 它抱怨:Cannot find name 'map'. Did you mean 'Map'?
    • 不,你应该使用map。只需在文件顶部添加import {map} from 'rxjs/operators';
    • 所以我更新了这个问题。总而言之,上述方法会挂起我的浏览器。
    • 我认为浏览器挂起是因为您的列表中没有 /login 路由。在这种情况下,Angular 会将您重定向到主页,并且您的守卫执行的次数越来越多
    • 谢谢。我的愚蠢疏忽。在路线的末尾添加了一个全面的未找到路径。真的很感激。希望上述解决方案将在本月剩下的时间里继续工作,直到它变成完全奇怪的东西。 ;)
    猜你喜欢
    • 2016-10-23
    • 2018-12-22
    • 1970-01-01
    • 2017-10-15
    • 2020-11-14
    • 2016-11-10
    • 2019-02-05
    • 2018-03-24
    • 2020-12-22
    相关资源
    最近更新 更多