Yes, you can do this either to create a new account or to log in to it:
To create, read createUserWithEmailAndPassword Docs
createUserWithEmailAndPassword throws 3 exceptions:
You can handle this in onCompleteListener or onFailureListener
Here's an example where mAuth is a FirebaseAuth instance:
mAuth.createUserWithEmailAndPassword(email, password) .addOnCompleteListener( new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (!task.isSuccessful()) { try { throw task.getException(); } // if user enters wrong email. catch (FirebaseAuthWeakPasswordException weakPassword) { Log.d(TAG, "onComplete: weak_password"); // TODO: take your actions! } // if user enters wrong password. catch (FirebaseAuthInvalidCredentialsException malformedEmail) { Log.d(TAG, "onComplete: malformed_email"); // TODO: Take your action } catch (FirebaseAuthUserCollisionException existEmail) { Log.d(TAG, "onComplete: exist_email"); // TODO: Take your action } catch (Exception e) { Log.d(TAG, "onComplete: " + e.getMessage()); } } } } );
To log in, first read signInWithEmailAndPassword Docs .
signInWithEmailAndPassword throws two exceptions:
FirebaseAuthInvalidUserException : if the message does not exist or is disabled.FirebaseAuthInvalidCredentialsException : if password is incorrect
Here is an example:
mAuth.signInWithEmailAndPassword(email, password) .addOnCompleteListener( new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (!task.isSuccessful()) { try { throw task.getException(); } // if user enters wrong email. catch (FirebaseAuthInvalidUserException invalidEmail) { Log.d(TAG, "onComplete: invalid_email"); // TODO: take your actions! } // if user enters wrong password. catch (FirebaseAuthInvalidCredentialsException wrongPassword) { Log.d(TAG, "onComplete: wrong_password"); // TODO: Take your action } catch (Exception e) { Log.d(TAG, "onComplete: " + e.getMessage()); } } } } );
amrro source share