【问题标题】:Firebase - Auth custom token error auth/network-request-failedFirebase - 验证自定义令牌错误 auth/network-request-failed
【发布时间】:2016-11-25 13:00:55
【问题描述】:

我曾尝试过 Firebase 3.5 和 3.6,但有同样的错误。我已经阅读了一些 toipc 并且我已经删除了表单中的 type="submit" 并在项目中添加了域,但它总是报告。我已经用 Firefox 50 和 Chrome 54 对其进行了测试——我在 web 中创建了它。我的流程是:我调用 ajax 并接收数据 - 如果数据为真,它将返回 json include token 和 sucess = 1。在 Ajax 调用之后,如果有数据和 data.sucess = 1,我将使用 data.token 进行身份验证

身份验证/网络请求失败

如果网络错误(例如超时、连接中断或 发生了无法访问的主机)。

<form id="form-login" action="" enctype="application/x-www-form-urlencoded">
<!-- Username -->
    <input id="username" name="username" type="text" size="20"  />
<!-- Password -->
    <input id="password" name="password" autocomplete="off" type="password" size="20"  />
<input id="btn-login" class="h_submit" value="Login" />
$('#btn-login').click(function(event) {
        event.preventDefault();
        
        var username = $.trim(jInputUsername.val());
        var password = $.trim(jInputPassword.val());
        if (!username || !password) {
            alert("Error");
        } else {
            
            $.ajax({
                url: '', 
                type: "post",
                data: {'username': username, 'password': password}, 
                success: function(data, textStatus, jqXHR) {
                    //        
                    if ($.isPlainObject(data)) {
                        //    On error
                        if (data && data.succeed) {
                            firebase.auth().signInWithCustomToken(data.token).catch(function(error) {
                              var errorCode = error.code;
                              var errorMessage = error.message;
                              if (errorCode === 'auth/invalid-custom-token') {
                                alert('Token problem.');
                              }
                              alert(errorMessage);
                            });
                            var fullHref = decodeURI(window.location.href);
                            setTimeout(window.location.href = fullHref.replace(hash, ''), 1000);
                        }
                    }
                },
                error: function( jqXHR, textStatus) {
                    console.log('Username or passord wrong");
                }
                , dataType:'json'
            });
        }
    });

【问题讨论】:

  • 您也可以使用“.then”承诺,如下所示:firebase.auth().signInWithEmailAndPassword(email, password) .then(user =&gt; console.log(user)) .catch((error) =&gt; console.log(error));

标签: authentication firebase firebase-realtime-database


【解决方案1】:

出现此错误是因为 Auth 对象需要时间来初始化。尽管您使用的是timeout,但这并不能保证初始化成功。

根据 Firebase 文档here,检查登录是否成功并获取当前用户的推荐方法是在Auth 对象上设置观察者。因此,要解决此错误,请在 onAuthStateChanged 观察者内重定向 window.location.href。

希望这会有所帮助!

【讨论】:

  • 我明天会尝试测试它。默认,它没有setTimeout,我插入它是因为我认为firebase无法完成功能并报告问题所以我插入了它
  • 您不应该使用 timeout 来等待完成,因为您无法预测完成需要多长时间并且您不想等待这么长时间。几天前我遇到了同样的错误,这个方法解决了。
猜你喜欢
  • 1970-01-01
  • 2017-08-31
  • 1970-01-01
  • 2023-03-29
  • 2016-12-16
  • 2016-10-12
  • 2018-12-26
  • 2018-05-22
  • 1970-01-01
相关资源
最近更新 更多