【问题标题】:Firebase - getRedirectResult not working in Cordova browserFirebase - getRedirectResult 在 Cordova 浏览器中不起作用
【发布时间】:2018-02-04 10:56:51
【问题描述】:

我已将 Firebase 设置到我的 Cordova 应用程序以通过 Facebook 进行身份验证,由于我找到了一种解决方法,这在 Android 中运行良好。但是,在我的浏览器中,我无法让它工作。

问题:一旦我调用 signInWithRedirect(provider) 方法,它会重定向到 firebase 并正确登录(在 firebase 的仪表板上检查),然后它会重定向到我的 localhost:8000 但它永远不会在 getRedirectResult() 方法中停止。

没有显示控制台日志错误。

我的应用设置:

index.html

<script src="https://www.gstatic.com/firebasejs/4.3.0/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.1.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.1.1/firebase-auth.js"></script>

app.js

 var config = {
            apiKey: "*********",
            authDomain: "*********",
            databaseURL: "*********",
            projectId: "*********",
            storageBucket: "*********",
            messagingSenderId: "*********"
          };
firebase.initializeApp(config);

login_controller.js

var provider = new firebase.auth.FacebookAuthProvider();

            firebase.auth().signInWithRedirect(provider).then(function() {
                firebase.auth().getRedirectResult().then(function(result) {
                    // This gives you a Google Access Token.
                    // You can use it to access the Google API.
                    var token = result.credential.accessToken;
                    // The signed-in user info.
                    var user = result.user;
                    console.log('result '+ result);
                    debugger;

                  }).catch(function(error) {
                    // Handle Errors here.
                    var errorCode = error.code;
                    var errorMessage = error.message;
                    debugger;
                  });
                }).catch(function(error){
                    debugger;
                });

Firebase 配置

添加了 localhost 域

网络环境设置

Facebook 身份验证设置

我错过了什么?

【问题讨论】:

  • 在浏览器环境中,firebase.auth().getRedirectResult() 应该在firebase.auth().signInWithRedirect(provider) 调用之外调用。您还可以添加一个onAuthStateChanged 侦听器,当用户未登录时,调用signInWithRedirect,否则视为已登录。
  • 不起作用..如果我在外部调用 getRedirectResult 将在 signInWithRedirect 抛出和空结果之前运行。
  • getRedirectResult 如果不是从重定向登录返回,则将解析为空结果。通常,在您确认用户未通过onAuthStateChanged 侦听器登录后,当用户单击登录时,您应该调用signInWithRedirect。您添加 getRedirectResult 以获得该结果。但是您可能已经登录并调用它会得到一个空结果。
  • 你解决了这个问题吗?

标签: javascript cordova firebase firebase-authentication facebook-login


【解决方案1】:

我是这样设置的(和我写的plugin交织在一起):

当识别出用户未登录时调用此方法:

toggleWebViewWithAuth: function () {
    var win = function(d) {
        console.log("web view toggled, going to auth");

        auth.presentLogin().then(function() {
            auth.getResult();
        });
    };
    var fail = function(e) {
        console.log(e)
    }

    cordova.exec(win, fail, "NavigationPlugin", "toggleWebView", []);
}

auth.presentLogin 是我创建的 async 函数,如下所示:

presentLogin: async function () {
    var provider = new firebase.auth.FacebookAuthProvider();
    //provider.addScope('default');
    provider.addScope('email');
    //provider.addScope('pages_show_list');

    return firebase.auth().signInWithRedirect(provider);
},

同样在auth变量中是:

getResult: function () {
    firebase.auth().getRedirectResult().then(function(result) {

        if (result.credential) {
            // This gives you a Facebook Access Token. You can use it to access the Facebook API.
            var token = result.credential.accessToken;
            // ...
        }

        // The signed-in user info.
        app.user = result.user;

        console.log("result.user in presentLogin:", app.user);
    }).then(function() {
        //iosNav.getCurrentTabControllerName();
        iosNav.toggleWebView();
    }).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        alert("Error:", error);

        // The email of the user's account used.
        var email = error.email;
        // The firebase.auth.AuthCredential type that was used.
        var credential = error.credential;
        // ...
    });
}

所以现在回去阅读我发布的toggleWebViewWithAuth 方法,你会看到我是如何使用它的。这使得getRedirectWithResult 等待signInWithRedirect。所有这些都可以在插件之外配置(不使用cordova.exec,iOS 端的所有方法都是打开或关闭UIWebView) - 最重要的部分是你只需要确保使用我的presentLogin 方法我如何呈现它,在 then 块中调用 getResult

此外,对于您的 js 导入、firebase-app 和所有 firebase 模块 js 文件,使用全局 firebase.js 导入替换。你只需要:

<script>.../firebase-app.js</script>
<script>.../firebase-auth.js</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 2019-03-18
    • 2021-04-02
    相关资源
    最近更新 更多