Firebase Returning user object after account creation
Solution 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
Solution 2
Looks like firebase has made some changes on this method. The accepted solution needs a little fix:
firebase.auth().createUserWithEmailAndPassword(textUser, textPassword)
.then(function(data){
console.log('uid',data.user.uid)
//Here if you want you can sign in the user
}).catch(function(error) {
//Handle error
});
Now you get to acces the user via data.user
Hope this helps :)
Solution 3
The createUserWithEmailAndPassword()
function returns a so-called Promise, which has methods catch()
and then()
. You're already using catch()
to handle problems. To handle "non-problems", you need to use then()
:
firebase.auth().createUserWithEmailAndPassword(textUser, textPassword)
.then(function(user) {
console.log(user); // see https://firebase.google.com/docs/reference/js/firebase.User
})
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
return Result = "false";
// ...
});
See the reference documentation for createUserWithEmailAndPassword and for firebase.User.
PowerMan2015
Updated on June 05, 2022Comments
-
PowerMan2015 almost 2 years
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().onAuthStateChanged
function but i then receive a timeout errorIf 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>
-
Frank van Puffelen about 8 yearsFrom the doc you link: "On successful creation of the user account, this user will also be signed in to your application."
-
Ymmanuel about 8 yearsYou are correct...a change i didn't catch from the old version... corrected thanks
-
Frank van Puffelen about 8 yearsYeah, that behavior has changed completely. I get bitten by it all the time.
-
PowerMan2015 about 8 yearsim receiving a auth/network-request-failed error implementing the above. Debugging suggest that the .then promise is not called
-
PowerMan2015 about 8 yearsim receiving a auth/network-request-failed error implementing the above. Debugging suggest that the .then promise is not called
-
Ymmanuel about 8 yearsIf the then is not executed , the catch will...can you show the log of the error received in the catch???
-
Script Kitty about 8 yearsI'm having a similar problem. After account creation, I'm redirecting back into my main index.html but it appears I am not signed in.
-
Ymmanuel about 8 yearsauth/network-request-failed : Thrown if a network error (such as timeout, interrupted connection or unreachable host) has occurred.
-
Ymmanuel about 8 yearscan someone replicate it?
-
bojeil about 8 yearsAre you on a slow or unstable network? Try to test on other devices to make sure the problem is only local. Also, check your network log for the request status.
-
bojeil about 8 yearsHey Script Kitty, just in case you are not doing so, are you making sure to redirect back to your index.html when the promise resolves: firebase.auth().createUserWithEmailAndPassword(email, pass).then(function(user) { //redirect here });
-
PowerMan2015 almost 8 yearsSome strange results here, the network error persisted while i was using a <form action="return createUser();"> method with a submit button. I removed the action from the form and changed the submit input for a button input and it worked. I think the form in my case was pushing the user to an alternative url which included the get request from the form. Hope this help someone else
-
Adam Youngers about 6 yearsHas the promise returned changed? I'm not able to reach user.uid, instead I have to do something like data.user.uid.
-
tim.rohrer over 5 yearsUnless I'm missing something in the links, I don't see explicit reference describing the
uid
. Is it truly unique? random? How does Google/Firebase generate it? Still searching.... -
Keyur about 4 yearsThis is correct as per new changes. Thanks, saved my day.