Angular - waiting for Google authentication to complete

谁说我不能喝 提交于 2019-12-11 15:49:06

问题


I'm trying to get Google authentication working properly in Angular 4. The issue is that the code isn't waiting for a result from the Google auth pop-up window before continuing execution.

The actual login works, it's just controlling the next step that's causing me trouble. I've tried adding a "getLoginResult" method which returns an observable, but it didn't help.

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
     }
  }
}

In the 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));
    }
}

I also tried returning a result from 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;
    }
  }

来源:https://stackoverflow.com/questions/46030619/angular-waiting-for-google-authentication-to-complete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!