I'm trying to make a registration page with android using Authentication with Email and password and then registering the other data to the real time database. When i click "register" button the email and the other data is getting entered into the authentication. but the other data won't process into the realtime database.
I'm not seen any error either.
This is MainActivity.java file
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();
}
}
});
}
}
And this is User.java file
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;
}
}
Taskin youronCompletemethod. E.g.Toast.makeText(MainActivity.this,"Error: Registration failed!\n"+task.getException().toString(), Toast.LENGTH_LONG).show();