我使用Google身份验证成功登录和注销,但我的身份验证服务未返回登录状态。我试图在我的Auth Guard中读取它以确定我是否应该直接进入登录页面。我得到_googleAuth
未定义的错误(“TypeError:无法读取未定义的属性'isSignedIn'”)。Angular 2从服务获取Google身份验证状态
constructor(private router: Router, private _authService: AuthenticationService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this._authService.getAuthenticatedStatusAsObservable()
.map((result) => { ...
的描述性的命名服务方法是这样的:
getAuthenticatedStatusAsObservable(): Observable<boolean> {
if(this._googleAuth){
return Observable.of(this._googleAuth.isSignedIn.get());
}
else {return Observable.of(false) }
}
}
其他服务方法的工作。例如,我可以写出用户名。但是,当我试图获取状态(或用户名)作为可观察的,它不起作用。
为什么在构造函数中没有足够的认证服务?我假设我错过了使用observables的东西。
loadAuth()被调用的服务的构造函数:
loadAuth() {
// attempt to SILENT authorize
this.gapiLoad
.load('auth2')
.switchMap(() => this.authorize())
.do((googleAuth: gapi.auth2.GoogleAuth) => this.saveGoogleAuth(googleAuth))
.do((googleAuth: gapi.auth2.GoogleAuth) => this.listenToGoogleAuthSignIn(googleAuth))
.filter((googleAuth: gapi.auth2.GoogleAuth) => this.isSignedIn())
.filter((googleAuth: gapi.auth2.GoogleAuth) => this.hasAccessToken(googleAuth))
.map((googleAuth: gapi.auth2.GoogleAuth) => googleAuth.currentUser.get())
.subscribe((googleUser: gapi.auth2.GoogleUser) => {
this.zone.run(() => this.handleSuccessLogin(googleUser));
});
}
显示你是如何初始化'this._googleAuth'的? –
@RomanC添加了它。 thx – beachCode
它的前三行失败了,你应该调试。对不起,这是题外话 –