15

I'm building an Android app and I want my users to be Auth then added to the database. For now I'm only doing the Auth with an email and password, then I click register and the user is created in the Auth section in the Firebase console.

But I also want my users to be added in the database. I want to add more fields (such as name, address etc) which will be stored in the users database in Firebase.

I just don't really see how to do that. Anyone could explain please?

Thanks!

raphh
  • 618
  • 2
  • 8
  • 22
  • 1
    See http://stackoverflow.com/a/37420701/4695325 – Devid Farinelli Jul 01 '16 at 09:51
  • 2
    It's for adding additional properties, but not explaining how to create the user object in the database in the first place... – raphh Jul 01 '16 at 13:17
  • Do I need to create some sort of structure in the database (using the Firebase console)? As I said earlier, once I connect a new user, it's mentioned in the 'Auth' tab but the 'Database' tab is still empty. – raphh Jul 01 '16 at 13:24
  • Exactly, you have to create an entry in the database (not from the console, but you will se your data there). Your code should create this entry and store there the data for your user. – Devid Farinelli Jul 01 '16 at 13:27
  • The data record for the user is not automatically created. You'll have to do that in your code, as in the answer David linked or in this http://stackoverflow.com/q/31038611. – Frank van Puffelen Jul 01 '16 at 14:32
  • @FrankvanPuffelen Yes, that's what I want to do but I don't really understand the code in the documentation. Maybe you can give me some more precisions? – raphh Jul 04 '16 at 08:45
  • @FrankvanPuffelen I've just spend the whole day searching on stackoverflow about this issue and read several answers about it (some were yours actually) but all in Swift or Javascript, I'd like to know if it was possible to have an explanation about it in Java, as I'm building an Android app. Thank you! – raphh Jul 04 '16 at 13:30
  • @FrankvanPuffelen I went through this http://stackoverflow.com/questions/29975024/firebase-java-android-createuser-failing and it seems to be a way to `createUser` with more properties, however when I go to `my-firebaseurl.com/users/` I see nothing... I think I'm almost there! – raphh Jul 04 '16 at 14:00
  • which version firebase auth you are using ? – rainman Aug 23 '16 at 10:05
  • 1
    I do have the exact same problem. There is nothing on the web that explains how to add users and other information to the firebase. I am able to see the email in the database created automatically by the firebase. Now what if I want to add username, phone number, address etc to the firebase database. Please help ! – Mohit Khaitan Sep 14 '16 at 19:34

2 Answers2

7

You should create a node where you save your users, like this:

users: {
  userID1: {
    name: 'Raph'
  },
  userID2: {
    name: 'Devid'
  }
}

Every logged user has a unique uid that you can use as key. You can find it in the authToken when you log your user in.

NOTE this is just a normal node in Firebase that you use to save user's data. It is an alternative solution to the Firebase generated users.

Devid Farinelli
  • 7,514
  • 9
  • 42
  • 73
  • Ok, I created "users" by clicking on the green "+", so now when I go to http://.firebaseio.com/users/userID1, I have userID1 (the long string K9gdkDgfd...) with "null" written. Shouldn't I be able to see the email and the password like, the data from the users that just has been created? I don't really understand how to manage that from the code, I thought everything should be set up in the db (like, creating the structure manually then filling it with users when they create their account on the app) – raphh Jul 01 '16 at 13:39
  • And in my code, for now, I only do `auth.createUserWithEmailAndPassword(email, password)` – raphh Jul 01 '16 at 13:43
  • You can find those data (email, provider, name, but not the password) in the authToken when you log your user, and push them in firebase. Firebase internally creates users profile (see the link in my response) but it allow you to store only few data. If you want to save more information you should use this method. Hope I'm clear :) – Devid Farinelli Jul 01 '16 at 13:44
  • Do you use `AuthStateListener`? https://firebase.google.com/docs/auth/android/password-auth#before_you_begin – Devid Farinelli Jul 01 '16 at 13:50
  • No, should I ? I don't really understand its purpose. For now in the SignupActivity I only have `createUserWithEmailAndPassword` – raphh Jul 01 '16 at 13:59
  • Yep, you should :) `createUserWithEmailAndPassword` is used to create an account, `signInWithEmailAndPassword` is used to log a user in, and `AuthStateListener` is used to detect the authentication state to know if your user is logged in or not. I suggest reading this documentation, it explains it very well https://firebase.google.com/docs/auth/android/password-auth#create_a_password-based_account – Devid Farinelli Jul 01 '16 at 14:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116203/discussion-between-devid-farinelli-and-raphh). – Devid Farinelli Jul 01 '16 at 14:03
  • Ok, I think I get it. I'm connecting my user then displaying some infos (email) in the log and it matches. Now I want to add more properties. Can I do it inside the `createUserWithEmailAndPassword` method or should I use something else? I see in the doc how to get data but not how to set. – raphh Jul 01 '16 at 14:47
  • My previous answer explains how to manage the login flow. Keep in mind that `createUserWithEmailAndPassword` is a sign up, you do it only the first time. If you are already registered you should logi in, that's the usage of `signInWithEmailAndPassword`. Please read the documentation, everything is explained there, and if you have further questions please use the chat here: http://chat.stackoverflow.com/rooms/116203/discussion-between-devid-farinelli-and-raphh :) – Devid Farinelli Jul 01 '16 at 14:56
  • Hi Raphh, did you solved this? Does my answer helped? – Devid Farinelli Jul 12 '16 at 06:58
0

Note: this was answered 3 years ago - Firebase improved a lot. Also the implementation changed

Here is one way to store the registered details in database,

Map<String, String> parameters = new HashMap<>();
FirebaseDatabase mFirebaseInstance;
parameters.put(Constant.TAG_USER, strUsrS.trim());
parameters.put(Constant.TAG_PASS, strPassS.trim());
//use this if needed(pushId)
String pushId = mFirebaseInstance.getReference(YOUR TABLE NAME).getRef().push().getKey();
parameters.put(Constant.TAG_KEY, pushId.trim());

mFirebaseInstance.getReference(YOUR TABLE NAME).getRef().child(strUsrS.trim()).setValue(parameters);
E_net4
  • 27,810
  • 13
  • 101
  • 139
Arnold Brown
  • 1,330
  • 13
  • 28