52

I have integrated firebase auth with my android app. Lets say a user has a mail [email protected]. I want to add some extra information to the user like the name of the user, occupation and address. How can i connect the user auth table with my android app to do that?

Do i need to write any APIs for that?

5
  • Do want to store user information in a table like real database or would like to get personal information from mail id Commented Aug 22, 2016 at 10:23
  • 1
    Yes i want them like a real database. May be a nosql json will be enough or table will be good to carry on. Now I am calling auth.createUserWithEmailAndPassword to create the user and I am finding them in my console. Commented Aug 22, 2016 at 10:25
  • Have you gone through Real time database in firebase Commented Aug 22, 2016 at 10:28
  • firebase.google.com/docs/database/android/save-data refer this link it may helps you Commented Aug 22, 2016 at 10:29
  • It helped Thanks :) Commented Aug 22, 2016 at 10:36

3 Answers 3

32

First, create a users directory in db. Then, using user's unique id you get from authn process, store the user info under users/{userid}.

To achieve this, you need to get into the details of Firebase database. See here: https://firebase.google.com/docs/database/android/save-data

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to create custom uid like I want to create user with email and password but uid as 1
@MasterYoda there a reason why firebase database not using increment as their unique id. Instead they provide their own unique identifier
@teckwei , There is a node module named firebase-admin with which it is possible. Thanks. I got it few days back.
24

You do not need to write any custom code to do this. Firebase already has features you can use.

The first thing you'd need to do is ensure that users have access to only the data they store. To do this, go to Database/Rules and change your rules to this:

{
"rules": {
  "my_app_user": {
    "$uid": {
      ".write": "auth != null && auth.uid == $uid",
      ".read": "auth != null && auth.uid == $uid"
     }
   }
 }
}

Then, to save the new details in a Firebase database, do this:

        MyAppUser user = new MyAppUser();
        user.setAddressTwo("address_two");
        user.setPhoneOne("phone_one");
        ...

        mDatabaseReference.child("my_app_user").child(firebaseUser.getUid()).setValue(user).
                addOnCompleteListener(DetailsCaptureActivity.this,
                                   new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            ...

The name of the child my_app_user must match both in your code and the Firebase rules else it won't persist.

If everything goes as is supposed to, you should see the details in your database:

enter image description here

Comments

6

You have to create another database table say "user". On successful signin, signup for first time you have to create a new row in user table.

public static void writeNewUser(DatabaseReference databaseReference, String userId, String name, String email, int accountType) {
    User user = new User(name, email, accountType);

    databaseReference.child("users").child(userId).setValue(user);
}

You may refer https://github.com/firebase/quickstart-android/tree/61f8eb53020e38b1fdc5aaeddac2379b25240f3b/database

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.