Yes, you can do that either you create new account or signing in:
For creating, read createUserWithEmailAndPassword
's Docs
createUserWithEmailAndPassword
throws 3 exceptions:
FirebaseAuthWeakPasswordException
: if the password is not strong enough
FirebaseAuthInvalidCredentialsException
: if the email address is malformed
FirebaseAuthUserCollisionException
: if there already exists an account with the given email address.
You can handle that in onCompleteListener
or onFailureListener
Here an example where mAuth
is 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());
}
}
}
}
);
For signing in, read signInWithEmailAndPassword
's Docs first.
signInWithEmailAndPassword
throws two exceptions:
FirebaseAuthInvalidUserException
: if email doesn't exist or disabled.
FirebaseAuthInvalidCredentialsException
: if password is wrong
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());
}
}
}
}
);