Link Firebase Auth with Android Database

-1

I searched without much success how I can link the users that I create using firebase auth to a firebase database, I already have the auth module working and registering my users but it does so in the auth part and I need them to be stored in my database to be able to link it with other entities and assign more capos.

Thanks for your answers!

    
asked by Peter 04.11.2018 в 12:24
source

1 answer

-1

To link them to a database, when you do a push in your realtime database for example, you only have to do the following to get that user's unique ID and push the database for that user only

First let's declare the FirebaseAuth , get the UID and then send something to Firebase Database

private FirebaseAuth mAuth;

Inside the onCreate we initialize it and save the UID of the user

mAuth = FirebaseAuth.getInstance();
String userID = mAuth.getCurrentUser().getUid();

Then when we go to put something in our database of Firebase we send it under the id of the user

For example, if we want to put a product under a user we can do the following

We declare the implementation of the database in our Build Gradle (app module)

implementation 'com.google.firebase:firebase-database:16.0.4'

First we will declare the reference to the data base where we want to write in Firebase

private DatabaseReference mDatabase;

then in our onCreate THEN to declare the mAuth, not before because it is going to give null the userID since it is not yet the Auth instance of Firebase

mAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance().getReference(); //Creamos una referencia al root de la base de datos
String userID = mAuth.getCurrentUser().getUid();
mDatabase.child("Usuarios").child(userID).child("Productos").setValue("manzana"); //Dentro de nuestra base de datos vamos a crear la referencia donde queremos guardar el producto

This line will return the following in our database

Usuarios
|________ Ksl290DNjszJpUn30 (El id unico del usuario)
            |_____ Productos  (Los productos de ese usuario)
                     |__ "manzana" (el producto del usuario que ingresamos)   

Each instance of mAuth.getCurrentUser.getUid() in different devices creates a unique ID for each user depending on the provider where it is logged in, by google, by email and password or by another Firebase Auth provider. This instance remains the same for the same logged accounts and does not change.

Remember that to use FirebaseAuth there is something very important to write or read in the database that are the rules of Realtime Database or Firestore, you configure them in this tab

There are two ways, with write and read in true we do not care if there is a user logged in to read or write data. and with auth!=null we say that only users logged in with FirebaseAuth can read or write

Any user can read or write

{
  "rules": {
    ".read": true,
    ".write": true
   }
  }

Only authenticated users can read or write our database

  {
      "rules": {
        ".read": "auth!=null",
        ".write": "auth!=null"
       }
      }

It may be that in many cases this all the code well to send your data but your rules are not allowing to be screened or read of it, I recommend you check what you are using.

These are the authentication means you can enable with FirebaseAuth

You can enable the one you want, in this case I use email and password.

I leave you a video in which you can understand a bit more how everything works: Link

Where I enter the pushID you can put the UserID which is the same, and in that way, they would be stored under each user.

    
answered by 04.11.2018 в 20:38