Firebase Returning user object after account creation

firebase get user by uid
firebase create user with email and password web
firebase user specific data
firebase email verification javascript
firebase check if user is logged in
firebase auth signout not working
firebase logout
firebase get user password

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>

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

with Cloud Functions · Steps after migrating your app · Start using Google Analytics The Firebase user object represents a user account that has signed up for an Once a user account has been created, you can reload the user's Apps use these tokens much like they use the tokens returned from  Create a user. You create a new user in your Firebase project by calling the createUserWithEmailAndPassword method or by signing in a user for the first time using a federated identity provider, such as Google Sign-In or Facebook Login. You can also create new password-authenticated users from the Authentication section of the Firebase console, on the Users page, or by using the Admin SDK.

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 :)

Export data to BigQuery · Extend with Cloud Functions · Steps after migrating your app · Start Firebase accounts will trigger user creation events for Cloud Functions when: From the user data returned to your function, you can access the list of user attributes available in the newly created user's UserRecord object. Users in Firebase Projects. Contents. User properties. Sign-in providers. The current user. The user lifecycle. Auth tokens. The Firebase user object represents a user account that has signed up for an app in your project. Apps usually have many registered users, and every app in a project shares a user database.

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.

What I did is after the promise for the signup completes, I make use of the user object it returns to update the profile. Its pretty self explanatory. This barrier between the data in your Firebase and your users' email / password hash combinations is by design: we want to make it easier for you to (1) develop your application, (2) prevent any accidental user credential leaks, and (3) still give you total flexibility with how to store your user data in Firebase.

You know there's a logged in user, you just logged in, heck, you can even see the user object in chrome dev tools. Then why is it still returning undefined ? There's​  – Fill in Email & Password field, then click CREATE ACCOUNT button: – Open Firebase Project Console to check user: – Click VERIFY EMAIL button: Then check email: – Click link in the email to verify, the browser shows: – Go back to Android App, sign out, then sign in, the account is verified now: III. Source code

Firebase accounts trigger user creation events for Cloud Functions when: A user creates an Triggered by creation or deletion of a Firebase Auth user object. Contents. Before you begin. Create a password-based account. Sign in a user with an email address and password. Next steps. You can use Firebase Authentication to let your users authenticate with Firebase using their email addresses and passwords, and to manage your app's password-based accounts.

Integrate with Firebase Determine if AccountManager is for you; Decide what type of account to use; Request Integration with the user's accounts allows you to do a variety of things such as: This returns an array of Account objects. The lesson after that deals with writing a custom authenticator so that you can install  I plan to create a main tree named users which will include the name different users used as username. So, from each username will be included their data e.g. Full Name, Address, Phone No. I want to know how to get each user's data when they log in on their profile.

Comments
  • From the doc you link: "On successful creation of the user account, this user will also be signed in to your application."
  • You are correct...a change i didn't catch from the old version... corrected thanks
  • Yeah, that behavior has changed completely. I get bitten by it all the time.
  • im receiving a auth/network-request-failed error implementing the above. Debugging suggest that the .then promise is not called
  • Has the promise returned changed? I'm not able to reach user.uid, instead I have to do something like data.user.uid.
  • im receiving a auth/network-request-failed error implementing the above. Debugging suggest that the .then promise is not called