2017-10-22 98 views
0

我創建了一個用戶註冊的系統,一旦他們成功註冊,他們在表單中填寫的其他信息應該被添加到節點與他們的用戶ID在數據庫中。Firebase createUserWithEmailAndPassword不授權用戶登錄

在我createUserWithEmailAndPassword .then()回調,我補充說,應該寫入數據庫的功能。但是,註冊後,數據庫寫入總是拋出permission denied錯誤。我決定檢查在firebase.auth()。currentUser的值,以確保新用戶實際上是登錄,和他們。

當我加入了同一個數據庫寫入功能的火力點登錄的回調,它的工作。

這是我的代碼;有沒有人有任何想法爲什麼認證無效?

//Declared above the createUser function 
function logUser(user, signUpPhoneNumber, department){ 
     database.ref('users/' + user.uid).set({ 
     phone: signUpPhoneNumber, 
     department: department 
    }); 
} 

firebase.auth().createUserWithEmailAndPassword(email, password).then(function(user){ 

     console.log("Email: " +firebase.auth().currentUser.email) //this prints the correct currentUser information. 
     return user; 
     }).then(function(user){ 
     console.log("Logged in: " + firebase.auth().currentUser.email); //this also prints correct user email info. 
     //signUpPhoneNumber and department declared elsewhere. 
     logUser(firebase.auth().currentUser.uid, signUpPhoneNumber, department); 
     }).catch(function(error) { 
     // Handle Errors here. 
     var errorCode = error.code; 
     var errorMessage = error.message; 
     // [START_EXCLUDE] 
     if (errorCode == 'auth/weak-password') { 
      alert('The password is too weak.'); 
     } else { 
      alert(errorMessage); 
     } 
     console.log(error); 
     // [END_EXCLUDE] 
     }); 

據我瞭解,創建用戶的功能,應在用戶自動登錄,而currentUser不爲空,但不知怎麼的權限不斷被剝奪(數據庫規則是需要認證的默認規則)。

安全規則是:

{ 
    "rules": { 
    ".read": "auth != null", 
    ".write": "auth != null" 
    } 
} 

我還測試了在註冊的回調和onAuthStateChanged回調後發現,在註冊後回調的AUTH狀態變化的火災,但把數據庫的寫在onAuthStateChanged回覆在註冊時觸發不起作用。 (雖然通過常規登錄觸發,但它仍然有效)。

+0

請修改您的問題以包含相關的安全規則。爲了進行測試,您可能還需要設置'「.WRITE」:真',看看它寫的'user.uid'。 –

+0

我的壞,安全規則是默認的(AUTH!= NULL)。正如我所說的,當從登錄回調中調用該函數時,該函數調用成功,並將用戶標識放置在具有正確信息的用戶節點下的數據庫中。 – zavtra

+1

你使用的是什麼版本的firebase.js?版本爲'4.5.2'存在問題,其中一些授權權限未正確傳遞。它們應該在4.6.0版本中修復。 – bojeil

回答

0

的問題是與火力地堡4.5.2。更新到4.6.0工作。