I've encountered a strange issue in Firebase. When I create a new user with the same password as an existing user, the new user is logged in under the previous user's account and displays all their information. However, upon logging out and signing back in with the new user's info, everything appears correctly. There was also a case where previously the new user would overwrite the existing user's path in Firebase, but that seems to be resolved now.
Below is my code snippet for creating a new user:
createNewUser () {
// Creating a new user with email and password.
var email = this.newUserEmail;
var password = this.newUserPassword;
firebaseAuth.createUserWithEmailAndPassword(email, password).catch(function(error) {
// Error handling.
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
});
});
});
// Redirect to home page.
router.push({ path: '/' });
}
}
}
The newly created user lands on the main home page, triggering the following code within the created() {}
method:
// Checks for user and changes authentication state to true.
firebaseAuth.onAuthStateChanged(user => {
console.log(store.state.authentication);
console.log(user);
store.dispatch('checkUser', user);
})
This action dispatches the following function:
checkUser (context, user) {
if (user.uid) {
context.commit('authUser', user);
// Fetching the user's isRetailer property from Firebase.
firebase.database()
.ref('/users/' + user.uid + '/isRetailer/')
.once('value').then(snapshot => context.commit('isRetailer', {
value: snapshot.val()
}));
// Retrieving current user's name from Firebase and storing it 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');
}
},
In summary, I'm creating a new user in Firebase, saving their data in the database, and updating Vuex state based on retrieved data from Firebase. However, when creating a user with a completely new password, nothing happens, accompanied by a specific console error message.
Unexpectedly, logging in with a reused password results in the new user being logged into the wrong account, overwriting the existing user's details in Firebase. This perplexing issue seems to stem from not retrieving the user after account creation using a promise. Your insight on this matter would be greatly appreciated.
UPDATE: Upon further testing, signing in with my credentials triggers an overwrite of my information in Firebase with another user's data. Subsequent logins continue displaying the other user's information. A strange scenario indeed.
Thank you for your help!