0

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;
}
}
1
  • To find the cause of the problem, log/raise the error from the Task in your onComplete method. E.g. Toast.makeText(MainActivity.this,"Error: Registration failed!\n"+task.getException().toString(), Toast.LENGTH_LONG).show(); Commented Aug 6, 2018 at 14:27

1 Answer 1

1

https://firebase.google.com/docs/database/security/

Did you checked your project's realtime database security rules? At first Google give you 2 option. One of them locked mode that no 3rd party allowed, other one is test mode allow for all. You can use test mode while developing but you should make your own rules for security reasons.

Sign up to request clarification or add additional context in comments.

2 Comments

I used the Test mode. Here is my security rules ` service cloud.firestore { ` match /databases/{database}/documents { match /{document=**} { allow read, write; } } } `
Those rules are for Cloud Firestore. The Firebase Realtime Database is a separate database, which has its own security rules that you can edit through console.firebase.google.com/u/0/project/_/database/_/rules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.