【发布时间】:2017-11-19 16:17:11
【问题描述】:
我已使用 Google 身份验证成功登录和注销,但我的身份验证服务未返回登录状态。我正在尝试在我的 Auth Guard 中阅读它以确定我是否应该直接访问登录页面。我收到_googleAuth 未定义的错误(“TypeError: Cannot read property 'isSignedIn' of undefined”)。
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) }
}
}
其他服务方法也有效。例如,我可以写出用户名。但是,当我尝试将状态(或用户名)作为 observable 获取时,它不起作用。
为什么在构造函数中没有足够的身份验证服务?我想我错过了一些关于使用 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 添加了它。谢谢
-
前三行某处出现故障,您应该调试。抱歉,这是题外话
标签: angular google-api angular2-services google-authentication