Home > database >  Register User using Firebase Authentication - problem with registration user
Register User using Firebase Authentication - problem with registration user

Time:04-16

I'm trying to create a chat app and I have to register users and I'm doing it with Firebase. Once I have entered all the data I click on register and I get the message:

You can't register with this email or password

Then it doesn't go successful.

I don't think there is anything wrong with the code. I have the emulator connected to the internet, I have connected firebase to the app, I don't know if I should check other things.

I imported this project from github. Maybe I did something wrong in the process? Can you explain what I could have done wrong?

Error in Log:

E/Auth: Unable to create user

com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The email address is badly formatted.

at com.google.android.gms.internal.firebase-auth api.zzti.zza(com.google.firebase:firebase-auth@@21.0.3:28)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Register");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    username = findViewById(R.id.username);
    email = findViewById(R.id.email);
    password = findViewById(R.id.password);
    btn_register = findViewById(R.id.btn_register);

    firebaseAuth = FirebaseAuth.getInstance();

    // When register is clicked check if fields are empty and if password is longer than 6 characters and call register method
    btn_register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String txt_username = username.getText().toString();
            String txt_email = email.getText().toString();
            String txt_password = password.getText().toString();

            if (TextUtils.isEmpty(txt_username) || TextUtils.isEmpty(txt_email) || TextUtils.isEmpty(txt_password)) {
                Toast.makeText(RegisterActivity.this, "All fields are required", Toast.LENGTH_SHORT).show();
            }
            else if (txt_password.length() < 6) {
                Toast.makeText(RegisterActivity.this, "Password must be at least 6 characters", Toast.LENGTH_SHORT).show();
            }
            else {
                register(txt_username, txt_email, txt_password);
            }
        }
    });
}

private void register(final String username, final String email, final String password) {
    // If register task is successful add a reference to Users
    firebaseAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
                        assert firebaseUser != null;
                        String userid = firebaseUser.getUid();

                        reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);

                        HashMap<String, String> hashMap = new HashMap<>();
                        hashMap.put("id", userid);
                        hashMap.put("username", username);
                        hashMap.put("imageURL", "default");
                        hashMap.put("status", "offline");
                        hashMap.put("search", username.toLowerCase());

                        reference.setValue(hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful()) {
                                    Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                    startActivity(intent);
                                    finish();
                                }
                            }
                        });
                    }
                    else {
                        Toast.makeText(RegisterActivity.this, "You can't register with this email or password", Toast.LENGTH_SHORT).show();
                    }
                }
            });
}

CodePudding user response:

When a task fails, it contains an exception with details about the cause of the problem. You should log that exception, so that you can find and fix the root cause:

@Override
public void onComplete(@NonNull Task<AuthResult> task) {
    if (task.isSuccessful()) {
        ...
    }
    else {
        Log.e("Auth", "Unable to create user", task.getException()); //            
  • Related