Firebase身份验证EmailAuthProvider:将匿名用户与凭据链接失败,并使用过期令牌

时间:2017-01-17 07:15:36

标签: firebase firebase-authentication

我正在使用匿名用户的firebase身份验证。当用户在网站上注册时,我们会链接他们的凭据。

以下是代码:

//Create the email and password credential, to upgrade the anonymous user.    
let credential = firebase.auth.EmailAuthProvider.credential(email, visitorId);

//Link the credential to the currently signed in user (the anonymous user).
auth.currentUser.link(credential).then((user) => {

     console.log("anonymous visitor successfully upgraded");

    }, 

    (error) => {

        //error upgrading the anonymouse user
        console.log("Error upgrading anonymous visitor", error);
});

Here is documentation of this approach

直到今天,这一直很有效。现在我突然收到错误代码:auth / network-request-failed。

如果我在Fiddler中检查失败的请求,我会找到响应:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalid",
    "message": "TOKEN_EXPIRED"
   }
  ],
  "code": 400,
  "message": "TOKEN_EXPIRED"
 }

}

奇怪的是,如果我在请求中解码令牌,则到期日期将在未来(大约提前1小时,这是其令牌的默认时间长度)。在生成令牌的时间与尝试将其链接到用户之间几乎没有延迟。那么为什么失败?

我的研究:

The issue here sounds similar, but I has not resolved it for me.

1 个答案:

答案 0 :(得分:0)

firebase支持团队要求的第一件事是该问题的简化演示。我应该先做到这一点。我的错。为了其他的好处,我将在下面包含演示代码。它没有问题。所以我们的问题显然与我们的实施有关。

我回到了网络请求。当我意识到响应firebase.auth()。currentUser.link(凭证)发送的网络请求显示为已取消时,第一条线索出现了。使用chrome:// net-internals /#events,我找到了 - > net_error = -3(ERR_ABORTED)。

这意味着在返回firebase请求之前,某些内容正在重定向页面。单步执行代码后,我发现Google代码管理器HTML代码段正在触发,其中包含单击特定元素时的重定向。

删除了该问题并解决了问题。我希望这个验尸证明对其他人有用。

<html>
    <head>
        <meta charset="utf-8">
        <script src="https://www.gstatic.com/firebasejs/3.6.2/firebase-app.js"></script>
        <script src="https://www.gstatic.com/firebasejs/3.6.2/firebase-auth.js"></script>
        <script>
        
        window.config = {
            apiKey: "<apikey>",
            authDomain: "<authDomain>",
            databaseURL: "<databaseUrl>",
          };
          

          //initialize the app
           firebase.initializeApp(window.config);
    
            firebase.auth().onAuthStateChanged((user) => {
              if (user) {
                // User is signed in.
                console.log("User is signed in:" + user.uid);
              } else {
                // No user is signed in.
                console.log("no user so sign in anonymously");
                firebase.auth().signInAnonymously().catch((error) => {
            			console.log("marketing.visitor:" + error.code + " " + error.message);
            	});
              }
            });
    
            function signInWithEmailAndPassword(){
                if(event.preventDefault){
                    event.preventDefault();
                }else{
                    event.returnValue = false; // for IE as dont support preventDefault;
                }
                
                var myElement = document.getElementById("lead_email");
                console.log(myElement.value);
                
                //Create the email and password credential, to upgrade the anonymous user.
                let credential = firebase.auth.EmailAuthProvider.credential(myElement.value, firebase.auth().currentUser.uid);
                
                //Links the credential to the currently signed in user (the anonymous user).
                firebase.auth().currentUser.link(credential).then((user) => {
                    
                    console.log("anonymous visitor successfully upgraded");

                }, 
                
                (error) => {
                   console.log("error upgrading anonymous user", error);
                });
                
                
                return false;
            }
    
            
        </script>
        
        
    </head>
    <body>
        <form onsubmit="signInWithEmailAndPassword()">
          <label class="form-field" for="lead_email">Email Address:</label>
        <input data-name="lead_email" id="lead_email" maxlength="256" name="lead_email" required="required" type="email">
        
        <input type="submit" value="Submit">
        </form>   
    </body>
</html>