1
Talk, guys. All right?
I am developing a web application with Firebase and as the auth() method only accepts e-mail and password, I am updating the displayname and saving the remaining data (Cpf, for example) in Firestore, at the time of user creation.
So far, so good. However, I tried to redirect you to the new page, AFTER these processes (displayname and Firestore).
First, I called the functions displayname, Firestore and redirect (I used window.location.href) in auth(’s . then). createUserWithEmailAndPassword(email, password), but redirected before performing the processes.
And then I tried to trap them into Predomises, but it didn’t work out so well.
document.getElementById('buttonSignupUser').addEventListener('click', function() {
var email = document.getElementById('inputEmail').value;
var password = document.getElementById('password').value;
var password2 = document.getElementById('password2').value;
if (password == password2){
firebase.auth().createUserWithEmailAndPassword(email, password)
.then(function() {
console.log('user created');
saveFirestore();
}).then(function(){
addDisplayName();
}).then(function(){
redirectSignup();
})
.catch(function(error) {
if (error.code == 'auth/invalid-email') {
console.log('Por favor, digite um e-mail válido');
} else if (error.code == 'auth/email-already-in-use') {
console.log('Esse e-mail já está sendo utilizado');
}
});
} else {
console.log('As senhas devem ser iguais');
}
})
This is the function to save the complementary data in the Firestore:
function saveFirestore(){
var email = document.getElementById('inputEmail').value;
var name = document.getElementById('inputName').value;
var cpf = document.getElementById('inputCPF').value;
var user = firebase.auth().currentUser;
var uid;
if (user != null) {
uid = user.uid;
}
firebase.firestore().collection('usuario').doc(uid).set({
CPF: cpf,
Email: email,
Nome: name,
Id: uid
})
console.log('saved firestore');
}
This is the function to update the displayname value
function addDisplayName(){
var name = document.getElementById('inputName').value;
firebase.auth().currentUser.updateProfile({
displayName: name
}).then(function() {
console.log('Display name added');
}).catch(function(error) {
console.log(error);
});
}
And lastly, this is to redirect the user
function redirectSignup(){
window.lotacion.href='logado.html'
}
I spent the day trying to solve this, I read about chaining Promises, Promises.all, async/await, I tried to use an if/Else...but as I study Javascript recently, many subjects are still complex and so I could not solve it.
I appreciate the attention, guys. Hugs
Speak, Edson. Beauty? A colleague helped me today and reached this conclusion

 firebase.auth().createUserWithEmailAndPassword(email, password)
 .then(() => saveFirestore())
 .then(() => addDisplayName())
 .then(() => redirectSignUp())
 .catch(err => {
 console.log(err);
 });

Worked perfectly– rodriguesgr.p
Very cool that managed to solve, seems a good solution. Unfortunately I could not help you.
– Edson Camargo
Imagine, it helped me yes...
– rodriguesgr.p