ThorNiLs
ThorNiLs

Reputation: 55

Firebase store user variables?

I am using Firebase for a beginner project of mine, and I am confused by the documentation that is provided. I have initialized an authentication form, and it's working. But I do not understand how to link it to the realtime database?

I understand that (user) is the currently logged it user, but how do I store information on the current user?

In my app, each user has a Balance(eg $100). How do I store the user's balance in their username, do I need to use the realtime databse?

Upvotes: 2

Views: 2779

Answers (1)

user3476154
user3476154

Reputation:

Basically, When a new user registers you'll get access to his UID through user.uid, you can use that UID to store info in the realtime database.

You can create a structure like this.

root: {
  users: {
    $uid: {
      // Store all your custom info here.
      balance: 100
    }
  }
}

And for every user you'd write their info to "users/uid". That should make saving and fetching info fairly trivial.

And to get the current user's balance all you'd have to do is observe "users/uid/balance".

Create an user.

firebase.auth().createUserWithEmailAndPassword(email, password)
.then(function(user){
  alert(user.uid)
}).catch(function(error) {
    console.log(error.message)
});

Reading and writing data

Upvotes: 3

Related Questions