【发布时间】:2016-06-18 13:03:35
【问题描述】:
我在这里阅读了指南:https://angular.io/docs/ts/latest/guide/router.html
看起来很简单,但是,我不确定如何在身份验证保护 (canActivate) 中使用 angularfire2 的身份验证。我试过的是:
AuthService
import { Injectable } from '@angular/core';
import { AngularFire } from 'angularfire2';
@Injectable()
export class AuthService {
private user: any;
constructor(private af: AngularFire) {
this.af.auth.subscribe(user => {
this.user = user;
})
}
get authenticated(): boolean {
return this.user ? true : false;
}
}
AuthGuard
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router, private authService: AuthService) { }
canActivate(): Observable<boolean> | boolean {
if (this.authService.authenticated)
return true;
this.router.navigate(['/login']);
return false;
}
}
我还向引导提供程序添加了 AuthService。
这种工作正常,但是,我的主要问题是当我刷新(或最初加载)具有AuthGuard 的页面时,它总是将我重定向到登录页面,因为AuthGuard 不等待身份验证响应。有没有办法等待身份验证完成(即使它失败)并然后检查用户是否通过身份验证?
【问题讨论】:
-
看到这个答案,它已经正确实现了auth guard stackoverflow.com/a/37889258/652850
-
并不能真正解决我的 angularfire2 问题。
标签: angular angularfire