我正在创建一个允许用户登录并注册的应用程序。目前,我正在使用Firebase来存储此数据,但我需要的不仅仅是电子邮件和密码。我能够将多个字段存储到实时数据库,但是我的登录页面使用的是带有Firebase的身份验证。反正两者有联系吗?
登录页面
//creating a new user
firebaseAuth.signInWithEmailAndPassword(username, password)
.addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Login Successful", Toast.LENGTH_LONG).show();
startActivity(new Intent(getApplicationContext(), FAQ.class));
finish();
} else{
Toast.makeText(MainActivity.this, "Login Error", Toast.LENGTH_LONG).show();
}
}
});
}
注册页面
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//firebaseAuth =FirebaseAuth.getInstance();
userN=(EditText)findViewById(R.id.userN);
password2=(EditText)findViewById(R.id.password2);
gender2=(EditText)findViewById(R.id.gender2);
headInjury2=(EditText)findViewById(R.id.headInjury2);
Smoker2=(EditText)findViewById(R.id.Smoker2);
dateOfBirth2=(EditText)findViewById(R.id.dateOfBirth2);
submitBtn2=(Button)findViewById(R.id.submitBtn2);
user= new User();
reff= FirebaseDatabase.getInstance().getReference().child("User");
submitBtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int date=Integer.parseInt(dateOfBirth2.getText().toString().trim());
user.setUserN(userN.getText().toString().trim());
user.setPassword2(password2.getText().toString().trim());
user.setGender2(gender2.getText().toString().trim());
user.setHeadInjury2(headInjury2.getText().toString().trim());
user.setSmoker2(Smoker2.getText().toString().trim());
user.setDOB(date);
reff.push().setValue(user);
Toast.makeText(Register.this, "Successfully registered", Toast.LENGTH_LONG).show();
}
});
}
答案 0 :(得分:0)
要在实时数据库中存储有关用户的其他数据,通常需要将该信息存储在用户的UID下。
所以你会有类似的东西
Users: {
uidOfCaitlin: {
firstName: "Caitlin",
lastName: "Daly McDowell",
dob: "2020-03-10"
},
uidOfFrank: {
firstName: "Frank",
lastName: "van Puffelen",
dob: "1970-03-10"
}
}
要存储这样的数据,您需要执行以下操作:
DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference().child("Users");
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
usersRef.child(uid).setValue(user);
这个主题已经涉及很多了,所以我也建议您查看: