Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

Angular - waiting for Google authentication to complete

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;
    }
  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...