Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
99
rated 0 times [  106] [ 7]  / answers: 1 / hits: 16406  / 8 Years ago, thu, june 2, 2016, 12:00:00

Im trying to create a user within Firebase and then create a user profile within the database on a web server. I have implemented the following code which creates the user quite nicely. However im not sure on how to receive the user id (which i need for a unique ID) to create the database structure. Is there a way to return a user object when the createUserWithEmailAndPassword is called?



I have tried to implement a firebase.auth().onAuthStateChangedfunction but i then receive a timeout error



If you havn't gathered this is for a web app.



<script>
function createUser() {
var Result = true;
var textUser = document.getElementById('userName').value;
var textPassword = document.getElementById('userPassword').value;
var textAccountID = document.getElementById('accountRef').value;
var textDateCreated = document.getElementById('dateCreated').value;
var textDisplayName = document.getElementById('displayName').value;
var UID;

firebase.auth().createUserWithEmailAndPassword(textUser, textPassword).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
return Result = false;
// ...
});

writeUserData(UID, textDisplayName, textAccountID, textDateCreated);

return Result;

}

function writeUserData(userId, displayName, accountID, dateCreated) {
firebase.database().ref('User/' + userId).set({
userId:{
AccountID: accountID,
Created: dateCreated,
Name: displayName}
});
}


</script>

More From » firebase

 Answers
1

In order to get the user id in the client side you should do this:



firebase.auth().createUserWithEmailAndPassword(textUser, textPassword)
.then(function(user){
console.log('uid',user.uid)

//Here if you want you can sign in the user
}).catch(function(error) {
//Handle error
});


as it is described here:



https://firebase.google.com/docs/reference/js/firebase.auth.Auth#createUserWithEmailAndPassword



Returns
non-null firebase.Promise containing non-null firebase.User


[#61921] Tuesday, May 31, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ciarajourneyv

Total Points: 428
Total Questions: 95
Total Answers: 90

Location: Maldives
Member since Sat, Feb 11, 2023
1 Year ago
;