Home > Mobile >  Writing data to Firebase database
Writing data to Firebase database

Time:12-17

Writing an uber clone through Android Studio, first time using Firebase. However, every time I click register user I get an error and nothing is entered.

When I'm on the emulator, I go to the DriverLoginActivity, enter my details for signup, no matter what is entered I get the Please check username and password requirements. But they all match, email format & password is over 6 digits. No matter what I do, I can't get it to register new users to the database

Firebase is connected, with dependancies all set up. JSON imported and correct SHA1 key. I've tried it on emulator and on my phone so I know its not the internet. Cannot figure it out for the life of me.

Firebase is set to Realtime database, with email and password authentification. Read and write rules are also set to true.

ANY insight would be hugely appreciated.

MainActivity.java

public class MainActivity extends AppCompatActivity {
private Button mDriver, mCustomer;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getSupportActionBar().setTitle("Hippo");

    //Call Buttons
    mDriver = findViewById(R.id.driver);
    mCustomer = findViewById(R.id.customer);

   mDriver.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent intent = new Intent(MainActivity.this, DriverLoginActivity.class);
            startActivity(intent);
            finish();
            return;
        }
    });

    mCustomer.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent intent = new Intent(MainActivity.this, CustomerLoginActivity.class);
            startActivity(intent);
            finish();
            return;
        }
    });





}

}

DriverLoginActivity.java

public class DriverLoginActivity extends AppCompatActivity {
private EditText mEmail, mPassword;
private Button mLogin, mRegister;

private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener firebaseAuthListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    FirebaseDatabase database = FirebaseDatabase.getInstance();



    FirebaseDatabase.getInstance().getReference("Users").child("Drivers");;

    mAuth = FirebaseAuth.getInstance();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_driver_login);
    getSupportActionBar().setTitle("Hippo Drivers");
    mAuth = FirebaseAuth.getInstance();
    firebaseAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            if (user != null) {
                Intent intent = new Intent(DriverLoginActivity.this, MapActivity.class);
                startActivity(intent);
                finish();
                return;
            }
        }
    };

    mEmail = findViewById(R.id.email);
    mPassword = findViewById(R.id.password);

    mLogin = findViewById(R.id.login);
    mRegister = findViewById(R.id.register);

    mRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final String email = mEmail.getText().toString();
            final String password = mPassword.getText().toString();
            Log.d("Done","Email and password to string");
            mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(DriverLoginActivity.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if(!task.isSuccessful())
                    {

                        Toast.makeText(DriverLoginActivity.this, "Error - Please check username and password requirements", Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        String user_id = mAuth.getCurrentUser().getUid();
                        DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child("Drivers").child(user_id);
                        current_user_db.setValue(true);
                    }
                }
            });
        }
    });

    mRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final String email = mEmail.getText().toString();
            final String password = mPassword.getText().toString();
            mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(DriverLoginActivity.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if(!task.isSuccessful())
                    {
                        Toast.makeText(DriverLoginActivity.this, "Error - Please check username and/or password", Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        //REGISTERED
                    }
                }

            });
        }
    });
}

@Override
protected void onStart()
{
    super.onStart();
    mAuth.addAuthStateListener(firebaseAuthListener);
    FirebaseUser currentUser = mAuth.getCurrentUser();

}

@Override
protected void onStop()
{
    super.onStop();
    mAuth.removeAuthStateListener(firebaseAuthListener);
}

CodePudding user response:

When a task fails, it contains an exception with more information about what went wrong. You should always log that exception to learn the cause of the problem:

public void onComplete(@NonNull Task<AuthResult> task) {
    if(!task.isSuccessful())
    {
        Log.e("Auth", "Creating user failed", task.getException()); //            
  • Related