Cosmin Zahariea
Cosmin Zahariea

Reputation: 73

Firebase Realtime Database Configuring

I'm trying to create in firebase a realtime database after a user sign in/log in for his profile.I made the authentification firebase,it's showing me that the user is in the firebase authetication.I tried a lot of tutorials but nothing works. How can I connect the authentication and the realtime database to make a user profile.

Rules:

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

User class:

   public String firstName;
    public String secondName;
    public String uid;
    public String email;
    public User(){

    }



    public User (String firstName, String secondName, String uid, String email) {
        this.firstName = firstName;
        this.secondName = secondName;
        this.uid=uid;
        this.email=email;
    }

    public User (String firstname, String secondname) {
    }

    public String getFirstName () {
        return firstName;
    }

    public String getSecondName () {
        return secondName;
    }

    public String getUid () {
        return uid;
    }

    public String getEmail () {
        return email;
    }

    public void setFirstName (String firstName) {
        this.firstName = firstName;
    }

    public void setSecondName (String secondName) {
        this.secondName = secondName;
    }

    public void setUid (String uid) {
        this.uid = uid;
    }

    public void setEmail (String email) {
        this.email = email;
    }
}

And the UserDetails class:

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class UserDetails extends AppCompatActivity {
    private FirebaseAuth auth;
  private DatabaseReference reference;
  private FirebaseDatabase database;
  private FirebaseAuth.AuthStateListener authStateListener;
    private FirebaseUser user;
    private EditText firstName,secondName;
    private Button saveInfo;



    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_user_details );
        firstName=(EditText)findViewById ( R.id.firstname );
        secondName=(EditText)findViewById ( R.id.secondname );
        saveInfo=(Button)findViewById ( R.id.data );
        auth=FirebaseAuth.getInstance ();
        reference.addValueEventListener ( new ValueEventListener () {
            @Override
            public void onDataChange (@NonNull DataSnapshot dataSnapshot) {
                String firstname=dataSnapshot.getValue (User.class)
            }

            @Override
            public void onCancelled (@NonNull DatabaseError databaseError) {

            }
        } )





    }

}


I deleted some code at the DataSnapshot because it didn't work.What can I do? I used a lot of tutorials but nothing is written in the database. I want to be write in the database the userId,first name and the second name

Upvotes: 0

Views: 66

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598603

The code reference.addValueEventListener reads from the database, while you're asking about writing.

To write to the database, you can do something like:

User user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
  DatabaseReference userRef = FirebaseDatabase.getInstance().getReference(user.getUid());
  userRef.child("firstname").setValue("Value of firstname field");
  userRef.child("secondname").setValue("Value of secondname field");
}

This creates a structure where the user data is stored under their UID, which is what your security rules enforce.


You can merge the two write operations in a single setValue() call with:

Map<String,Object> values = new HashMap<>();
values.put("firstname", "Value of firstname field");
values.put("secondname", "Value of secondname field");
userRef.setValue(values);

If you don't want to use maps of values, you can create a Java class to represent a user's properties. The simplest version of a class with these two JSON properties is:

class UserProperties {
  public string firstname;
  public string secondname;
}

And then initialize and write it with:

UserProperties userprops = new UserProperties();
userprops.firstname = "Value of firstname field";
userprops.secondname = "Value of secondname field";
userRef.setValue(userprops);

Upvotes: 1

Related Questions