我正在使用Firebase设置注册/登录应用程序。在我的实时数据库中,我有两个孩子,“供应商”和“食客”,都在“客户”父项下。当用户注册时,它将正确地将其信息保存到Firebase身份验证选项卡,但是当我转到“实时数据库”选项卡时,它不会在任何子级中显示用户ID。
我已经尝试过DatabaseReference来将userID存储到正确的子级,但是似乎没有将userID存储到数据库,它仅将用户存储到Authentication。
public class VendorLoginActivity extends AppCompatActivity {
private EditText emailEditText;
private EditText passwordEditText;
private Button loginButton;
private Button registrationButton;
private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener fbAuthenticationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vendor_login);
emailEditText = findViewById(R.id.email_edit_text);
passwordEditText = findViewById(R.id.password_edit_text);
loginButton = findViewById(R.id.login_button);
registrationButton = findViewById(R.id.registration_button);
firebaseAuth = FirebaseAuth.getInstance();
fbAuthenticationListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser fbUser = FirebaseAuth.getInstance().getCurrentUser();
if (fbUser != null ){
startActivity(new Intent(VendorLoginActivity.this, MainActivity.class));
finish();
return;
}
}
};
// Registration Button Clicked
registrationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String emailUser = emailEditText.getText().toString();
final String passwordUser = passwordEditText.getText().toString();
firebaseAuth.createUserWithEmailAndPassword(emailUser, passwordUser).addOnCompleteListener(VendorLoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()){
Toast.makeText(VendorLoginActivity.this, R.string.registration_fail_toast, Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(VendorLoginActivity.this, R.string.registration_success_toast, Toast.LENGTH_SHORT).show();
String client_id = firebaseAuth.getCurrentUser().getUid();
DatabaseReference client_db = FirebaseDatabase.getInstance().getReference().child("Clients").child("Vendor").child(client_id);
client_db.setValue(true);
}
}
});
}
});
// Login Button Clicked
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String emailUser = emailEditText.getText().toString();
final String passwordUser = passwordEditText.getText().toString();
firebaseAuth.signInWithEmailAndPassword(emailUser, passwordUser).addOnCompleteListener(VendorLoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()){
Toast.makeText(VendorLoginActivity.this, R.string.login_failed_toast, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(VendorLoginActivity.this, R.string.login_success_toast, Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
@Override
protected void onStart() {
super.onStart();
firebaseAuth.addAuthStateListener(fbAuthenticationListener);
}
@Override
protected void onStop() {
super.onStop();
firebaseAuth.removeAuthStateListener(fbAuthenticationListener);
}
}
我希望userId显示在Firebase Realtime Database子级“ Vendor”或“ Eater”中(取决于用户首先在“欢迎”屏幕上按下的按钮),但是Firebase Realtime Database在任何以下项中均不显示任何userId孩子是“供应商”或“食者”。