首页 文章

在单击表单之前,离子firebase onAuthStateChanged不会触发

提问于
浏览
0

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

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

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

What I don't understand is why it isn't checking for the authorization until I click on the screen.

Edit: 单击“使用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或在设备/模拟器中运行

Edit 2 我've added a console log to check where I'我到了 . 当我使用重定向(在浏览器上)登录时,页面返回到app.components.ts并检测到用户已更改...但在我点击表单之类的元素之前页面不会移动 .

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

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

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

相关问题