2016-08-30 61 views
1

我有一個Firebase應用程序連接到monaca CLI和OnsenUI。我正在嘗試創建一個用戶並以相同的操作登錄它們。 我可以成功地創建一個用戶但我不能登錄。當我登錄他們在我收到以下錯誤使用Firebase登錄用戶時出現「auth/user-not-found」

auth/user-not-found 

There is no user record corresponding to this identifier. The User may have been deleted 

我證實,新用戶是在分貝...這是我的代碼註冊和登錄

//signup function stuff 
var login = function() { 
    console.log('got to login stuff'); 
    var email = document.getElementById('username').value; 
    var password = document.getElementById('password').value; 

    //firebases authentication code 
    firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) { 
    // Handle Errors here. 
    var errorCode = error.code; 
    var errorMessage = error.message; 
    console.log('User did not sign up correctly'); 
    console.log(errorCode); 
    console.console.log(errorMessage); 
    }); 

    firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) { 
    console.log(error.code); 
    console.log(error.message); 
    }); 

    fn.load('home.html'); 


}; 
+0

創建一個用戶自動記錄該用戶,因此您不需要分開登錄它們。 –

回答

4

你有你所謂的競爭條件在你的流量。

當您致電createUserWithEmailAndPassword() Firebase 開始創建用戶帳戶。但是這可能需要一些時間,所以瀏覽器中的代碼會繼續執行。

立即繼續使用signInWithEmailAndPassword()。由於Firebase可能仍在創建用戶帳戶,因此此通話將失敗。

總體解決方案這種類型的情況是鏈中的呼叫在一起,例如用then()

firebase.auth().createUserWithEmailAndPassword(email, password).then(function(user) { 
    firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) { 
    console.log(error.code); 
    console.log(error.message); 
    }); 
}).catch(function(error) { 
    // Handle Errors here. 
    var errorCode = error.code; 
    var errorMessage = error.message; 
    console.log('User did not sign up correctly'); 
    console.log(errorCode); 
    console.console.log(errorMessage); 
}); 

但安德烈庫爾已經評論:自動創建用戶登錄他們了,所以在這種情況下,你可以這樣做:

firebase.auth().createUserWithEmailAndPassword(email, password).then(function(user) { 
    // User is created and signed in, do whatever is needed 
}).catch(function(error) { 
    // Handle Errors here. 
    var errorCode = error.code; 
    var errorMessage = error.message; 
    console.log('User did not sign up correctly'); 
    console.log(errorCode); 
    console.console.log(errorMessage); 
}); 

您可能會很快也要detect whether the user is already signed中,當他們到您的網頁。爲此,你會使用onAuthStateChanged。從文檔:

firebase.auth().onAuthStateChanged(function(user) { 
    if (user) { 
    // User is signed in. 
    } else { 
    // No user is signed in. 
    } 
}); 
+0

感謝您的好評。我已經實施了您的更改,不再面臨問題。我的下一個問題是,在嘗試使用.push()之後,如何獲得「拒絕權限」錯誤。就好像我的用戶沒有登錄 – IWI

相關問題