首页 > 解决方案 > 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

标签: androidfirebase-realtime-database

解决方案


代码从数据库中reference.addValueEventListener 读取,而您正在询问是否编写。

要写入数据库,您可以执行以下操作:

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");
}

这将创建一个结构,其中用户数据存储在其 UID 下,这是您的安全规则强制执行的。


您可以在一次setValue()调用中合并两个写入操作:

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

如果您不想使用值映射,可以创建一个 Java 类来表示用户的属性。具有这两个 JSON 属性的类的最简单版本是:

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

然后初始化并编写它:

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

推荐阅读