我正在尝试在Angular 4中正常使用Google身份验证。问题是代码在继续执行之前不会等待Google身份验证弹出窗口的结果。
实际登录有效,它只是控制导致我麻烦的下一步。我已经尝试添加一个“getLoginResult”方法,该方法返回一个observable,但它没有帮助。
login() {
this._authService.getLoginResult()
.subscribe(results => {
if(results === true) {
// want to do something here
}
else {
// this always gets executed b/c auth window hasn't returned
}
}
}
在authService中......
getLoginResult(): Observable<boolean> {
if(this.signIn()) {
return Observable.of(true);
}
else {
return Observable.of(false);
}
}
signIn() {
const signOptions: gapi.auth2.SigninOptions = {scope: SCOPES };
if (this._googleAuth) {
Observable.fromPromise(this._googleAuth.signIn(signOptions))
.subscribe((response: any) => this.handleSuccessLogin(response), error => this.handleFailedLogin(error));
}
}
我也尝试从signIn返回结果。
signIn(): boolean {
const signOptions: gapi.auth2.SigninOptions = {scope: SCOPES };
if (this._googleAuth) {
Observable.fromPromise(this._googleAuth.signIn(signOptions))
.subscribe(response => {
if(response === true) {
return true;
}
else {
return false;
}
});
}
else {
return false;
}
}