我正在尝试使用通过电子邮件和密码进行身份验证的android系统创建一个注册页面,然后将其他数据注册到实时数据库中。当我单击“注册”按钮时,电子邮件和其他数据将输入到身份验证中。但其他数据不会处理到实时数据库中。
我也没有看到任何错误。
这是MainActivity.java文件
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView txtName, txtEmail, txtPassword, txtMobile;
private Spinner spinner;
private ProgressBar progressBar;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtName = findViewById(R.id.txtName);
txtEmail = findViewById(R.id.txtEmail);
txtPassword = findViewById(R.id.txtPassword);
txtMobile = findViewById(R.id.txtMobile);
progressBar = findViewById(R.id.progressBar);
progressBar.setVisibility(View.GONE);
spinner = findViewById(R.id.spinner);
mAuth=FirebaseAuth.getInstance();
findViewById(R.id.btnRegister).setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btnRegister:
registerUser();
break;
}
}
@Override
protected void onStart() {
super.onStart();
if(mAuth.getCurrentUser()!=null){
//handle the already login user
}
}
private void registerUser(){
final String name = txtName.getText().toString().trim();
final String email = txtEmail.getText().toString().trim();
String password = txtPassword.getText().toString().trim();
final String mobile = txtMobile.getText().toString().trim();
final String gender = spinner.getSelectedItem().toString();
if(name.isEmpty()) {
txtName.setError("Name required!");
txtName.requestFocus();
return;
}
if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
txtEmail.setError("Enter a valid email!");
txtName.requestFocus();
return;
}
if(password.isEmpty()) {
txtPassword.setError("Password Required!");
txtPassword.requestFocus();
return;
}
if(password.length()<6) {
txtPassword.setError("Strong password required");
txtPassword.requestFocus();
return;
}
if(mobile.isEmpty()) {
txtMobile.setError("Mobile Required!");
txtMobile.requestFocus();
return;
}
progressBar.setVisibility(View.VISIBLE);
mAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
//Store additional fields into firebase database
User user = new User(
name,
email,
mobile,
gender
);
FirebaseDatabase.getInstance().getReference("Users")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
progressBar.setVisibility(View.GONE);
if(task.isSuccessful()){
Toast.makeText(MainActivity.this,"User Registered Successfully!", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"Error: Registration failed!", Toast.LENGTH_LONG).show();
}
}
});
}else{
Toast.makeText(MainActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
}
这是User.java文件
public class User {
public String name, email, phone, gender;
public User(){
}
public User(String name, String email, String phone, String gender) {
this.name = name;
this.email = email;
this.phone = phone;
this.gender = gender;
}
}
答案 0 :(得分:1)
https://firebase.google.com/docs/database/security/
您是否检查了项目的实时数据库安全规则?首先,Google给您2个选择。其中一种是第三方不允许的锁定模式,另一种是测试模式允许所有人使用。您可以在开发时使用测试模式,但是出于安全原因,您应该制定自己的规则。