Firebase Auth - with Email and Password - Check user already registered
Clash Royale CLAN TAG#URR8PPP
Firebase Auth - with Email and Password - Check user already registered
I want to check when a user attempts to signup with createUserWithEmailAndPassword()
in Firebase user Authentication method, this user is already registered with my app.
createUserWithEmailAndPassword()
8 Answers
8
To detect whether a user with that email address already exists, you can detect when the call to createUserWithEmailAndPassword ()
fails with auth/email-already-in-use
. I see that @Srinivasan just posted an answer for this.
createUserWithEmailAndPassword ()
auth/email-already-in-use
Alternatively, you can detect that an email address is already used by calling fetchSignInMethodsForEmail()
(previous called fetchProvidersForEmail()
).
fetchSignInMethodsForEmail()
fetchProvidersForEmail()
When the user trying to create an user with same email address, the task response will be "Response: The email address is already in use by another account."
mFirebaseAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
@Override
public void onComplete(@NonNull Task<AuthResult> task)
if(task.isSuccessful())
//User registered successfully
else
Log.i("Response","Failed to create user:"+task.getException().getMessage());
);
For that you need to use fetchProvidersForEmail (String email) method. But, now it's having some issue. I was not able to find the proper solution only by email. If you find please update the answer.
– Srinivasan
Oct 13 '16 at 10:08
@Srinivasasan Sure I will update that's what I need like in Gmail, type - email address then if email is available then proceed to password
– Coolbub
Oct 13 '16 at 10:12
@Srinivasan Thanks for help!!!!!!!!!!!!!...+1
– Gowthaman M
Nov 25 '17 at 7:38
First of all, you need to make sure you have that restriction enabled in Firebase console (Account and email address settings). Take a look at @Srinivasan's answer.
Then, do this in your java code:
firebaseAuthenticator.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
@Override
public void onComplete(@NonNull Task<AuthResult> task)
if (!task.isSuccessful())
if (task.getException() instanceof FirebaseAuthUserCollisionException)
Toast.makeText(SignUpActivity.this, "User with this email already exist.", Toast.LENGTH_SHORT).show();
else
sendVerificationEmail();
startActivity(new Intent(SignUpActivity.this, DetailsCaptureActivity.class));
// ...
);
This is where the trick happens:
if (task.getException() instanceof FirebaseAuthUserCollisionException) {
Toast.makeText(SignUpActivity.this,
"User with this email already exist.", Toast.LENGTH_SHORT).show();
Several exceptions can be thrown when registering a user with email and password, but the one we are interested in is the FirebaseAuthUserCollisionException
. As the name implies, this exception is thrown if the email already exists. If the exception thrown is an instance of this class, let the user know.
FirebaseAuthUserCollisionException
As a practice of @Frank's answer here is the code of using fetchProvidersForEmail()
fetchProvidersForEmail()
private boolean checkAccountEmailExistInFirebase(String email)
FirebaseAuth mAuth = FirebaseAuth.getInstance();
final boolean b = new boolean[1];
mAuth.fetchProvidersForEmail(email).addOnCompleteListener(new OnCompleteListener<ProviderQueryResult>()
@Override
public void onComplete(@NonNull Task<ProviderQueryResult> task)
b[0] = !task.getResult().getProviders().isEmpty();
);
return b[0];
I like this answer and @Frank's , but could you explain- Why are you declaring a boolean array ? instead of a single boolean?
– user3833732
Jan 18 at 17:04
thank you ^^, I think that was just a correction from the android studio for stating a final variable and using it in an anonymous method, no other need to use an array of boolean other than this reason here
– Dasser Basyouni
Jan 22 at 15:12
I was looking into this kind of condition where we can detect if user exists or not and perform registration and login. fetchProvidersForEmail
is best option right now. I have found this tutorial. Hope it helps you too!
fetchProvidersForEmail
private ProgressDialog progressDialog;
progressDialog.setMessage("Registering, please Wait...");
progressDialog.show();
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
@Override
public void onComplete(@NonNull Task<AuthResult> task)
//checking if success
if (task.isSuccessful())
//Registration was successfull:
Toast.makeText(RegistrationActivity.this, "Successfully registered!", Toast.LENGTH_LONG).show();
else
//Registration failed:
//task.getException().getMessage() makes the magic
Toast.makeText(RegistrationActivity.this, "Registration failed! " + "n" + task.getException().getMessage(), Toast.LENGTH_LONG).show();
progressDialog.dismiss();
);
Add below code to MainActivity.java file.When user attempt to register with the same email address a message "The email address is already used by another account" will pop up as a Toast
mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>()
@Override
public void onComplete(@NonNull Task<AuthResult> task)
if(!task.isSuccessful())
Toast.makeText(MainActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
if(task.isSuccessful())
Toast.makeText(MainActivity.this, "Sign up successfull", Toast.LENGTH_SHORT).show();
);
You do not have to do anything because the backend of Firebase will do the job.
Unless you are referring to reauthenticating of the app.
Take a scenario for an example, w
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Is there any way check user is already registered, without sending the password?
– Coolbub
Oct 13 '16 at 9:50