android - 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
解决方案
代码从数据库中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);
推荐阅读
- firebase - 如何将未来放入 initState 中?
- html - 网站上的 Facebook 赞按钮仅在访问者登录 FB 时有效
- forms - 导航反应本机
- r - 从 R 中的 zip 文件中读取数据时,它会破坏先前读取的数据
- linux - 运行可执行文件时的 Termux 错误
- python-3.x - Django rest 允许获取但不允许列出
- python - 从多个类、xpath 或 css 选择器中按索引提取文本
- ios - 我使用 cocoapods 制作静态框架
- javascript - 从 Firebase Firestore 获取嵌套数据
- clion - 接受代码完成时插入一个额外的空格