Я должен зарегистрировать пользователей из моего веб-приложения. Но после регистрации одного пользователя второй пользователь всегда удаляет данные текущего пользователя и попадает под предыдущий uid.
Вот мой код.
<script>
// Already Initialized Firebase
var text = document.getElementById('name');
var email = document.getElementById('email');
var password = document.getElementById('password');
var button = document.getElementById('button');
var profilePic = document.getElementById('profilePic');
function registerUser(){
var Name = text.value;
var Email = email.value;
var Password = password.value;
var Image = profilePic.files[0];
window.alert(Name)
firebase.auth().createUserWithEmailAndPassword(Email, Password).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// ...
});
var user = firebase.auth().currentUser;
var uid = user.uid;
var dbRefUid = firebase.database().ref().child('Web App').child('Users');
dbRefUid.set(uid);
var dbRefName = firebase.database().ref().child('Web App').child('Users').child(uid).child('Name');
var dbRefEmail = firebase.database().ref().child('Web App').child('Users').child(uid).child('Email');
var dbRefPassword = firebase.database().ref().child('Web App').child('Users').child(uid).child('Password');
var dbRefProfilepicUrl = firebase.database().ref().child('Web App').child('Users').child(uid).child('Profilepicture URL');
dbRefName.set(Name);
dbRefEmail.set(Email);
dbRefPassword.set(Password);
var imageRoot = firebase.storage().ref('users/' + "profile");
imageRoot.put(Image);
var uploadTask = imageRoot.put(Image);
// Register three observers:
// 1. 'state_changed' observer, called any time the state changes
// 2. Error observer, called on failure
// 3. Completion observer, called on successful completion
uploadTask.on('state_changed', function(snapshot){
// Observe state change events such as progress, pause, and resume
// Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED: // or 'paused'
console.log('Upload is paused');
break;
case firebase.storage.TaskState.RUNNING: // or 'running'
console.log('Upload is running');
break;
}
}, function(error) {
// Handle unsuccessful uploads
}, function() {
// Handle successful uploads on complete
// For instance, get the download URL: https://firebasestorage.googleapis.com/...
uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
dbRefProfilepicUrl.set(downloadURL);
console.log('File available at', downloadURL);
});
});
firebase.auth().signOut().then(function() {
window.alert("Sign Out")
// Sign-out successful.
}).catch(function(error) {
// An error happened.
});
}
</script>
Мое требование - зарегистрировать разных пользователей с уникальным идентификатором uid через базу данных firebase и функцию аутентификации firebase. Оба места должны быть одинаковыми. Все работает. Но не так, как я хочу.