2017-07-10 48 views
1

在我的登錄頁我有一個按鈕時調用這個函數(Firebase | Ionic)GoogleAuth在完成前延遲迴復錯誤返回?

googleauth(){ 
 
    if(this.auth.signInGoogle()){ 
 
     alert("1"); 
 
     if(this.auth.getcurrentUser().displayName){ 
 
      alert("2"); 
 
      this.gameStatus.players[0].name = this.auth.getcurrentUser().displayName; 
 
      alert("3"); 
 
      this.navCtrl.setRoot(HomePage); 
 
     } 
 
    }else{ 
 
     alert("googleauth else"); 
 
    } 
 
    }

在我的身份驗證提供者,這是signInGoogle()函數

signInGoogle(){ 
 
    \t this.googleplus.login({ 
 
     'webClientId' : '', 
 
     'offline' : true 
 
    }) 
 
    \t .then((res)=>{ 
 
    \t \t const firecreds = firebase.auth.GoogleAuthProvider.credential(res.idToken); 
 
     firebase.auth().signInWithCredential(firecreds).then((success)=>{ 
 
      alert("auth1"); 
 
      return true; 
 
     }).catch((err)=>{ 
 
      alert('Firebase auth failed ' + err); 
 
     }) \t \t 
 
    \t }).catch((err)=>{ 
 
    \t \t alert('Error:' + err); 
 
     return false; 
 
    \t }); \t 
 
    }

這是什麼顯示v我的手機上IA警報當我點擊該按鈕:
googleauth其他
auth1

auth1高於return true,所以裏面if(this.auth.signInGoogle()){..}的東西應該叫,而是在else {..}部分被調用。
googleauth else提醒之前調用auth1提醒是否有像我需要使用的等待函數?函數在線程中運行?這怎麼會發生?我怎麼解決它?

謝謝你們提前


編輯
我的帳戶中火力認證頁面顯示
整個authProvider

import { Injectable } from '@angular/core'; 
 
import 'rxjs/add/operator/map'; 
 
import { GooglePlus } from '@ionic-native/google-plus'; 
 
import * as firebase from 'firebase/app'; 
 
import { GamestatusProvider } from '../../providers/gamestatus/gamestatus'; 
 
@Injectable() 
 
export class AuthProvider { 
 
    constructor(public googleplus: GooglePlus) {} 
 
    signInGoogle(){ 
 
    \t this.googleplus.login({ 
 
     'webClientId' : '..', 
 
     'offline' : true 
 
    }) 
 
    \t .then((res)=>{ 
 
    \t \t const firecreds = firebase.auth.GoogleAuthProvider.credential(res.idToken); 
 
     firebase.auth().signInWithCredential(firecreds).then((success)=>{ 
 
      alert("auth1"); 
 
      return true; 
 
     }).catch((err)=>{ 
 
      alert('Firebase auth failed ' + err); 
 
     }) \t \t 
 
    \t }).catch((err)=>{ 
 
    \t \t alert('Error:' + err); 
 
     return false; 
 
    \t }); \t 
 
    } 
 
    getcurrentUser(){ 
 
    \t return firebase.auth().currentUser; 
 
    } 
 

 
}

回答

0

export class LoginPage { 
 

 
    constructor(public navCtrl: NavController, public navParams: NavParams, public auth: AuthProvider, public gameStatus: GamestatusProvider) { 
 
    firebase.auth().onAuthStateChanged(firebaseUser => { 
 
     if(firebaseUser){ 
 
       this.navCtrl.setRoot(HomePage);   
 
     } 
 
    }); 
 
    } 
 

 
    ionViewDidLoad() { 
 
    console.log('ionViewDidLoad LoginPage'); 
 
    } 
 
    anonAuth(){ 
 
    this.auth.signInAnonym();  
 
    } 
 
    googleAuth(){ 
 
    this.auth.signInGoogle(); 
 
    } 
 
}

export class AuthProvider { 
 
    constructor(public googleplus: GooglePlus, public gameStatus: GamestatusProvider) {} 
 
    signInAnonym(){ 
 
    firebase.auth().signInAnonymously();   
 
    } 
 
    signInGoogle(){ 
 
    \t this.googleplus.login({ 
 
     'webClientId' : webClientIdGooglePlusApi, 
 
     'offline' : true 
 
    }) 
 
    \t .then((res)=>{ 
 
    \t \t const firecreds = firebase.auth.GoogleAuthProvider.credential(res.idToken); 
 
     firebase.auth().signInWithCredential(firecreds).then((success)=>{   
 
      return true; 
 
     }).catch((err)=>{ 
 
      alert('Firebase auth failed ' + err); 
 
     }) \t \t 
 
    \t }).catch((err)=>{ 
 
    \t \t alert('Error:' + err); 
 
     return false; 
 
    \t }); \t 
 
    } 
 

 
    signOut(){ 
 
    firebase.auth().signOut(); 
 
    } 
 
}

的工作,但我還是很想知道爲什麼我第一次嘗試didnt工作?