我在Firebase中遇到了一個非常奇怪的問題,如果我使用與之前用戶相同的密碼創建新用戶,正確的信息,但正在以先前的用戶帳戶登錄並顯示所有用戶以前的信息。Vue + Firebase:正在創建用戶但在其他用戶帳戶下登錄的用戶
我註銷,然後我登錄新的用戶信息,然後才顯示正確的信息。如果我使用相同的密碼,我還有一個新用戶覆蓋Firebase中以前的用戶路徑問題,但似乎無法重新創建。
我的代碼來創建一個新的用戶如下
createNewUser() {
// Create a new user with email and password.
var email = this.newUserEmail;
var password = this.newUserPassword;
firebaseAuth.createUserWithEmailAndPassword(email, password).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// ...
}).then(() => {
firebaseAuth.onAuthStateChanged(user => {
database.ref('/users/' + user.uid).set({
name: this.newUserName,
email: this.newUserEmail,
isRetailer: false,
isAdmin: false
});
});
});
// Push to home
router.push({ path: '/' });
}
}
}
然後將新創建的用戶取到下面的代碼在created() {}
方法運行
// Checks for a user and dispatches an action changing isAuthed state to true.
firebaseAuth.onAuthStateChanged(user => {
console.log(store.state.authentication);
console.log(user);
store.dispatch('checkUser', user);
})
最後的主網頁,這發出一個動作,其運行如下
checkUser (context, user) {
if (user.uid) {
context.commit('authUser', user);
// checkUser runs once when the app is initialized.
// Gets value of the user's isRetailer property from firebase.
firebase.database()
.ref('/users/' + user.uid + '/isRetailer/')
.once('value').then(snapshot => context.commit('isRetailer', {
value: snapshot.val()
}));
// Gets name of the current user from Firebase and will eventually store that in Vuex state.
firebase.database()
.ref('/users/' + user.uid + '/name/')
.once('value').then(snapshot => context.commit('getDisplayName', {
name: snapshot.val()
}));
} else {
console.log('No user currently logged in');
}
},
所以基本上我在做以下工作
- 使用Firebase創建一個新用戶。
- 將該用戶保存到Firebase數據庫。
- 根據我從Firebase獲得的一些數據更新Vuex狀態。
當我剛剛創建一個全新的密碼的新用戶,沒有任何反應,我在控制檯中得到以下內容。
然而,當我創建一個使用先前已創建密碼的另一個新用戶,它記錄了我在,但最後的用戶(即用新的密碼創建的)的信息下。但是,使用新密碼創建的用戶不會存儲在Firebase中。
這真的是一個bizzare問題,我真的不知道是什麼原因造成的。 uid錯誤是我在控制檯中遇到的唯一錯誤。創建用戶時,我不能使用承諾嗎?我之所以加入這個功能是因爲用戶並沒有被添加到Firebase中,因爲在帳戶創建後我無法抓住用戶。
真的可以使用另一組眼睛在這一個。
更新:當試圖用我的信息登錄時,似乎我的信息被firebase中的另一個用戶信息覆蓋。然後,我也會在應用程序中顯示所有用戶信息。然後每當我嘗試用我的電子郵件/密碼登錄時,我從現在開始以約翰史蒂夫的身份登錄。
謝謝!
好的。我已經在第一個回調中刪除了'firebaseAuth.onAuthStateChanged(user => {})'並將其替換爲'var user = firebaseAuth.currentUser;'。然後使用'router.push()'方法刪除第二個回調,並將'router.push()'移到回調函數之外。這似乎是擺脫我的錯誤。我會在這裏做更多的測試並看看。非常感謝!! – maxwellgover
現在我認爲這解決了我的問題。希望它有!謝謝你的領導:) – maxwellgover