Я предполагаю странное поведение при создании учетной записи. У меня нет сообщения об ошибке, но иногда firebase не создает пользователя, иногда firebase создает пользователя, но не записывает какие-либо данные в базу данных в реальном времени. Пожалуйста, помогите мне ..
function signUp() {
var userArtistName = document.getElementById("userArtistName").value;
var userEmail = document.getElementById("userEmail").value;
var userPassword = document.getElementById("userPassword").value;
firebase.auth().createUserWithEmailAndPassword(userEmail, userPassword).then((success) => {
var user = firebase.auth().currentUser;
var uid;
if (user != null) {
uid = user.uid;
}
var firebaseRef = firebase.database().ref();
var userData = {
userArtistName: userArtistName,
userEmail: userEmail,
userIsArtist: true,
userId: uid,
}
firebaseRef.child('users/' + uid).set(userData);
console.log('User successfully created!');
// code for replace user
}).catch((error) => {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
});
}
моя html форма:
<form action="#">
<h1>Hello Form</h1>
<span>Register now</span>
<input type="text" placeholder="Name" id="userArtistName"/>
<input type="email" placeholder="Email" id="userEmail" />
<input type="password" placeholder="Password" id="userPassword" />
<button onclick="signUp()">Registrieren</button>
</form>
правила fb:
Моя firebase. js файл:
// Your web app's Firebase configuration
var firebaseConfig = {
//fb config here
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
// Get a reference to firebase authenthification
const auth = firebase.auth();
function signUp() {
var userArtistName = document.getElementById("userArtistName").value;
var userEmail = document.getElementById("userEmail").value;
var userPassword = document.getElementById("userPassword").value;
firebase.auth().createUserWithEmailAndPassword(userEmail, userPassword).then((success) => {
var user = firebase.auth().currentUser;
var uid;
if (user != null) {
uid = user.uid;
}
var firebaseRef = firebase.database().ref();
var userData = {
userArtistName: userArtistName,
userEmail: userEmail,
userIsArtist: true,
userId: uid,
}
firebaseRef.child('users/' + uid).set(userData);
console.log('User successfully created!');
// code for replace user
}).catch((error) => {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
});
}
function signIn() {
var email = document.getElementById("txtEmail");
var password = document.getElementById("txtPassword");
auth.signInWithEmailAndPassword(email.value, password.value)
.then(userCredential => {
const userId = userCredential.user.uid;
return firebase.database().ref('/users/' + userId).once('value')
})
.then(snapshot => {
var userIsArtist = snapshot.val().userIsArtist;
if (!userIsArtist) {
//Redirect to login or other action or message
//signOut();
console.log(userIsArtist);
} else {
window.location.replace("main.html");
}});
// }).catch((error) => {
// // Handle Errors here.
// // .catch function to hide error in console. error tells UID actually. We don't want that
// var errorCode = error.code;
// var errorMessage = error.message;
// });
}
function signOut() {
auth.signOut()
console.log('Successfully signed out!');
window.location.replace('login.html');
}
auth.onAuthStateChanged(function (user) {
if (user) {
let user = firebase.auth().currentUser;
if(user != null){
// deactivating loading spinner for people, who are not logged in
document.getElementById("login-cover").style.display = "none";
}
var email = user.email;
document.getElementById('fb-email').innerHTML = "Welcome User : " + email;
console.log('Successfully logged in! ---- Active User: ' + email);
} else if (
// Not signed in, redirecting to login page
user == null &&
window.location.href == 'http://localhost/www.muego.com/main.html' ||
window.location.href == 'http://localhost/www.muego.com/songs.html') {
// Redirect user because session is not valid anymore
window.location.replace('login.html');
}
});
function resetPassword() {
var auth = firebase.auth();
var emailAddress = "admin@trash-mail.com";
auth.sendPasswordResetEmail(emailAddress).then(function () {
// Email sent.
}).catch(function (error) {
// An error happened.
});
}