簡體   English   中英

使用 firebase auth 注冊用戶會拋出錯誤,但仍然可以正確注冊

[英]Using firebase auth to register a user throws an error, but still registers correctly

我有一個相當稀疏的“register.html”頁面,我在其中讀取用戶 email 和密碼並通過createUserWithEmailAndPassword注冊。 我附加了一個.then()方法,它不會被調用。 我還附加了一個.catch()方法,調用該方法時會出現user is not defined錯誤。

首先, user is not defined似乎更適合登錄,而不是注冊。 重點是“定義一個新用戶!”

其次,我也有.onAuthStateChanged偵聽器,並且確實顯示用戶狀態在注冊后更改為已登錄。

第三,轉到 firebase 身份驗證面板,我確實看到了新創建的用戶!

為什么我的catch塊被調用,而不是then塊?

這是我的代碼:

auth.createUserWithEmailAndPassword(username, password)
                .then(function(){
                     //user signed in
                    console.log('auth', user, 'registered');
                    if(location.href == document.referrer){
                        location.href = "/website/index.html";
                    }
                    else{ location.href = document.referrer; }
                })
                .catch(function(error) {
                    var errorCode = error.code;
                    var errorMessage = error.message;
                    $('#passwordAlert').show();
                    $('#passwordError').text(errorMessage);

                    console.log("Registeration failure:", username);
                    console.log('\t',errorCode);
                    console.log('\t',errorMessage);
                    //alert user
                });

根據我在嘗試回答此問題時的經驗Firebase (web) - 在注冊時添加用戶數據

.then沒有被調用,因為createUserWithEmailAndPassword

創建與指定的 email 地址和密碼關聯的新用戶帳戶。 成功創建用戶帳戶后,該用戶也將登錄到您的應用程序。

所以“這個用戶也將登錄到你的應用程序”。 它將觸發.onAuthStateChanged並且出於 idk 原因,它只是使.then不再被調用。


調用.catch是因為未觸發onAuthStateChanged ,未發生登錄或注銷

4.0.0 之后,觀察者只在登錄或注銷時觸發。

應某人的要求,我嘗試添加更多日志記錄。 但是,我現在得到了我期望的結果。 我知道我沒有更改任何邏輯,只更改了console.log語句。 不過,現在問題已經解決了。 不確定這是 firebase 中的臨時故障(可能不太可能)還是我的錯誤(盡管我直到花了幾個小時查看我的 177 行 html/js 頁面、查看文檔等后才發布消息.)。

無論如何,問題解決了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM