【发布时间】:2017-03-15 03:06:33
【问题描述】:
我希望我的身份验证守卫等到我从 firebase 取回身份验证后再重新路由我。目前,auth Guard 检查首先启动。
我需要在 canActivate 中使其异步,或者只在调用后填充 canActivate。
auth.guard.ts
import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';
import { AuthService } from './auth.service';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService) {
console.log(this.authService.isAuthenticated); // returns false, doesn't async
}
canActivate() {
console.log(this.authService.isAuthenticated); // returns false
return this.authService.isAuthenticated;
}
}
auth.service.ts
...
import { AngularFire,
AuthProviders,
AuthMethods,
FirebaseListObservable,
FirebaseObjectObservable } from 'angularfire2';
...
@Injectable()
export class AuthService {
isAuthenticated: boolean = false;
// the problem is setting it to a bool, need to async it (( but this was from official documentation and Pluralsight tuts__
constructor(
public af: AngularFire,
private _router: Router,
private _route: ActivatedRoute ) {
this.af.auth.subscribe((auth)=>{
console.log('called after after auth guard');
if (auth == null) {
this._router.navigate(['/login']);
} else {
this.isAuthenticated = true;
}
}
});
}
那么如何让 AuthGuard 的 canActivate 等待异步布尔值?
我如何将它传递给 AuthGuard 的 canActivate() 函数?
【问题讨论】:
标签: angular angularfire2