【问题标题】:Using Ionic2 with firebase auth将 Ionic2 与 Firebase 身份验证一起使用
【发布时间】:2017-05-10 05:25:55
【问题描述】:

我尝试将 Ionic 2 与 firebase auth 一起使用:

signInWithFacebook(): firebase.Promise<FirebaseAuthState> {
    if (this.platform.is('cordova')) {
      console.log("trying to log in using cordova...");
      Facebook.login(['email', 'public_profile']).then(res => {
        console.log("Logged in. result here");
        const facebookCredential = firebase.auth.FacebookAuthProvider.credential(res.authResponse.accessToken);
        console.log(firebase.auth().signInWithCredential(facebookCredential));
        return firebase.auth().signInWithCredential(facebookCredential);
      });
    } else {
      return this.auth$.login({
        provider: AuthProviders.Facebook,
        method: AuthMethods.Popup
      });
    }
  }

此代码在 Web 中运行良好,但在 Android (Cordova) 中无法运行。

我调试了为什么登录按钮不起作用,我发现了这个错误:

例外:./HomePage 类 HomePage 中的错误 - 内联模板:6:0 原因:无法读取未定义的属性'then'......

登录。结果在这里

例外:未捕获(承诺):ReferenceError:firebase 不是 已定义 ReferenceError: firebase 未定义 在文件:///android_asset/www/build/main.js:42039:42 在 t.invoke...

关于第二个问题(firebase 未定义)我尝试过

`import firebase from 'firebase';`

但 Ionic 2 构建返回错误。

我刚刚使用 Ionic 2 使用了 Angularfire2 身份验证中的代码。

有人知道为什么会发生这种情况吗?

【问题讨论】:

    标签: cordova ionic2 cordova-plugins


    【解决方案1】:

    您没有返回Facebook.login()。所以我相信当你尝试在你的组件中访问它时它会返回错误。

      return Facebook.login(['email', 'public_profile']).then(res => {
    

    【讨论】:

    • 解决了这个问题,但是返回了错误的数据。我需要返回这个:firebase.auth().signInWithCredential(facebookCredential)
    • 如果firebase.auth().signInWithCredential(facebookCredential);返回一个带有你需要的信息的promise,你会在signInWithFacebook()返回的promise中收到它
    猜你喜欢
    • 2017-10-20
    • 2016-03-01
    • 2021-06-14
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2022-08-08
    相关资源
    最近更新 更多