1

I am trying to check if an email already exists in Firebase authentication, but I can find something for Java. I am trying to do something like searching in a list of emails and if that email is not in the database (emailNotExistsInDatabase(email)), then continue.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
Ioanaa 98
  • 25
  • 6

3 Answers3

2

In addition to the very complete response from Alex, another possible approach is to use a Callable Cloud Function that you call from your app.

Since we use the Admin SDK in Cloud Functions you can use the getUserByEmail() method.

The function would look like:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.checkEmailExists = functions.https.onCall((data, context) => {

    return admin.auth()
        .getUserByEmail(data.email)
        .then((userRecord) => {
            return { emailExists: true }
        })
        .catch((error) => {
            throw new functions.https.HttpsError('invalid-argument', "email doesn't exist");
        });

});

With this approach you don't need a specific Firestore collection. The Admin SDK will directly query the Auth service.

Look at the doc for instructions on how to call the Callable Cloud Function from your app.


Note that the above approach is valid if you want to check if a user exists from an application (e.g. an Android app).

If you already use the Admin SDK from a Java based server, you just have to use the getUserByEmail() method in your server code.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
0

There is no method inside the FirebaseAuth class that can help you check the existence of a user based on an email address. If you need that functionality you have to create it yourself. This means that when a user signs in for the first time into your app, then save user data in Firestore using a schema that looks like this:

db
|
--- users (collection)
     |
     --- $uid (document)
          |
          --- email: "user-email@gmail.com"

To check if a user with the user-email@gmail.com already exists, then you have to perform a query that looks like this in Java:

FirebaseFirestore db = FirebaseFirestore.getInstance();
Query queryByEmail = db.collection("users").whereEqualTo("email", "user-email@gmail.com");
queryByEmail.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                if (document.exists()) {
                    Log.d(TAG, "User already exists.");
                } else {
                    Log.d(TAG, "User doesn't exist.");
                }
            }
        } else {
            Log.d(TAG, task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

Another solution would be to use Query#count() method:

queryByEmail.count();

If the result is > 0 then it means that the user already exists, otherwise it doesn't exist.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks, I used the getUserByEmail and it solved the problem! In case my case I didn't have the email in uid collection in firebase, but I will add it since it's better that way! – Ioanaa 98 Jan 23 '23 at 18:23
0

If you want to check if a given email address is associated with a user profile in Firebase, you can call the fetchSignInMethodsForEmail API.

Note that this API gives malicious users a way to perform a so-called enumeration attack, so you can actually nowadays disable fetchSignInMethodsForEmail - in which case calling it from your app would fail.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I didn't have the fetchSignInMethodForEmail but I used the getUserByEmail and that solved my issue. I will take a look in the method you mentioned, because it's also a good approach. Thanks again! – Ioanaa 98 Jan 23 '23 at 18:24
  • `fetchSignInMethodForEmail` is a method in the client-side SDKs, while `getUserByEmail` is in the server-side/Admin SDKs. Either can be used to implement the use-case, but I had overlooked you were using non-Android Java. – Frank van Puffelen Jan 24 '23 at 04:39