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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…