在单击表单

时间:2018-01-20 21:27:56

标签: angularjs firebase ionic-framework firebase-authentication

我正在使用firebase构建一个用户登录Facebook的应用程序,我可以让电子邮件登录自动工作,但Facebook登录会将我返回到登录页面并等待,直到我在检测到之前单击表单认证已经改变了。

app.component.ts 中,我正在检测用户更改:

const unsubscribe = firebase.auth().onAuthStateChanged( user => {
  if (!user) {
    this.rootPage = 'LoginPage';
    unsubscribe();
  } else { 
    this.rootPage = 'HomePage';
    unsubscribe();
  }
});

login.ts 我正在使用以下代码执行登录(从不将我的root更改为HomePage):

facebookLogin() {
    this.authData.signInWithFacebook().then( res => {
      this.navCtrl.setRoot('HomePage');
    }, error => {
      //this.loading.dismiss().then( () => {
        let alert = this.alertCtrl.create({
          message: error,
          buttons: [
            {
              text: "Ok",
              role: 'cancel'
            }
          ]
        });
        alert.present();
      //});
    });
  }

在auth.ts中,我正在执行登录:

signInWithFacebook(): Promise<any> {
    if (this.platform.is('cordova')) {
      return this.facebook.login(['email']).then( res => {
        const facebookCredential = firebase.auth.FacebookAuthProvider.credential(res.authResponse.accessToken);
        firebase.auth().signInWithCredential(facebookCredential).then( (success) => {
          console.log("Firebase success: " + JSON.stringify(success));
        })
        .catch((error) => {
          console.log("Firebase failure: " + JSON.stringify(error));
        });
      });
    }
    else {
      return firebase.auth().signInWithRedirect(new firebase.auth.FacebookAuthProvider())
    }
  }
}

我最好的猜测是授权发生延迟,因此当我返回登录页面时,它会一直等到授权完成。

我不明白为什么在我点击屏幕之前没有检查授权。

修改 单击“使用Facebook登录”按钮后,控制台输出以下内容并运行facebookLogin()

  

[09:53:03] console.log:Angular正在开发模式下运行。   调用enableProdMode()以启用生产               模式。 [09:53:03] console.log:ionViewDidLoad LoginPage [09:53:03] console.warn:Native:试过调用   StatusBar.styleDefault,但Cordova不可用。确保               包含cordova.js或在设备/模拟器中运行[09:53:04] console.warn:Native:尝试调用SplashScreen.hide,但是Cordova是   无法使用。一定要包括               cordova.js或在设备/模拟器中运行

编辑2 我添加了一个控制台日志来检查我到达的位置。当我使用重定向(在浏览器上)登录时,页面返回到app.components.ts并检测到用户已更改...但在我点击表单等元素之前页面不会移动。 / p>

constructor(platform: Platform, statusBar: StatusBar, 
    splashScreen: SplashScreen) {
    //const authObserver = afAuth.authState.subscribe( user => {
    const unsubscribe = firebase.auth().onAuthStateChanged( user => {
      if (!user) {
        console.log('User auth state changed. Log them out.')
        this.rootPage = 'LoginPage';
        unsubscribe();
      } else { 
        //var currentState = $ionicHistory.currentStateName();
        console.log('User auth state changed. Send onwards.') // This runs.
        this.rootPage = 'HomePage'; // This doesn't run until I click an element.
        unsubscribe();
      }
    });

1 个答案:

答案 0 :(得分:0)

您似乎错过了返回最内层的承诺,这意味着在signInWithFacebook函数中, signInWithCredential已经返回后,最内层的承诺signInWithFacebook被执行

 return this.facebook.login(['email']).then( res => {
        const facebookCredential = firebase.auth.FacebookAuthProvider.credential(res.authResponse.accessToken);
//add return below
        return firebase.auth().signInWithCredential(facebookCredential).then( (success) => {
          console.log("Firebase success: " + JSON.stringify(success));
        })
        .catch((error) => {
          console.log("Firebase failure: " + JSON.stringify(error));
        });
     });