14

I am using Firebase Authentication with Email and Password

I would like to know if i can 'lookup' a user by email only while also not being signed in as a user

The reason I'd like to do this is, is to simply identify if I am already a user of the system, using an email address only

I looked at this older thread but it appears to be the previous version of Firebase

Is it possible to do this in the current Firebase, or my alternative would be to keep this information available (and open to all?) to find out if a given email is part of my system?

Community
  • 1
  • 1
7hacker
  • 1,928
  • 3
  • 19
  • 32
  • There is no built-in API in Firebase Authentication to look up a user by their email address. The common approach developers take is to solve this by modeling the information they need in the Firebase Database as in the thread you linked. – Frank van Puffelen Jul 20 '16 at 22:43
  • Thanks! If i maintain a node of user email addresses to simply get a Boolean True or False for existence, how do I protect this from not being read by anyone (public/world)? – 7hacker Jul 20 '16 at 22:47
  • You cannot prevent lookup of an individual email address, since that is exactly the use-case you're trying to implement. But you can make the list unreadable and only each individual email address readable to minimize exposure. – Frank van Puffelen Jul 20 '16 at 22:58
  • @FrankvanPuffelen would not the fetchSignInMethodsForEmail method work here? If so, how does one use it? – Zonker.in.Geneva Jun 28 '19 at 21:32

6 Answers6

17

I use fetchProvidersForEmail(email) and if the result return as empty array then, this email hasn't been use to sign up.

firebase.auth().fetchProvidersForEmail(email)
.then(providers => {
  if (providers.length === 0) {
    // this email hasn't signed up yet
  } else {
    // has signed up
  }
});
Doppio
  • 2,018
  • 12
  • 11
6

You can look up user information by email:

firebase.auth().getUserByEmail(email)
  .then(function(userRecord) {
    // See the UserRecord reference doc for the contents of userRecord.
    console.log('Successfully fetched user data:', userRecord.toJSON());
  })
  .catch(function(error) {
   console.log('Error fetching user data:', error);
  });

I'd like to make things more clear that this method does not exist — one could be looking for it in the firebase client library, in which it has never been available in the first place and it wouldn't be a good idea to have anyway. This method is part of the admin SDK, so in order to call the method, you need to run it on the server, and invoke it from the client. OP didn't scope the question to firebase client library, so my answer is still correct.

Retrieve user data

E_net4
  • 27,810
  • 13
  • 101
  • 139
Andrea
  • 553
  • 9
  • 12
  • The returned object's documentation can be found [on the UserRecord reference documentation from firebase](https://firebase.google.com/docs/reference/admin/java/reference/com/google/firebase/auth/UserRecord) – Ruben Szekér Jun 03 '21 at 07:35
  • This API does not exists [anymore]. – Salathiel Genese Sep 18 '21 at 05:09
  • 1
    @SalathielGenèse Where did you find mention to the API deprecation? I can still see it referenced here: https://firebase.google.com/docs/reference/admin/node/admin.auth.BaseAuth#getuserbyemail and documented here: https://firebase.google.com/docs/auth/admin/manage-users#retrieve_user_data – Andrea Sep 18 '21 at 11:56
  • Sorry @Andrea, you just called to my attention that JS vs. Dart SDK APIs differs with subtleties. Thank you. – Salathiel Genese Sep 18 '21 at 15:58
5

The new method of creating users with email password returns a value whether the given email address is already in use. See here

F0r3v3r-A-N00b
  • 2,903
  • 4
  • 26
  • 36
1

import { fetchSignInMethodsForEmail } from 'firebase/auth'

fetchSignInMethodsForEmail(auth, registerEamil).then((result) =>{
        console.log("result", result);
        if (result.length === 0) {
            Navigate("/authentication/select_role/" + 
      registerEamil)
        } else {
            Navigate('/')
        }
  
Usama ijaz
  • 21
  • 2
0

Server side option:

https://cloud.google.com/identity-platform/docs/reference/rest/v1/projects.accounts/lookup

POST https://identitytoolkit.googleapis.com/v1/projects/{targetProjectId}/accounts:lookup

{ "email": ["rodneydangerfield@stackoverflow.kom"] }
jprio
  • 97
  • 1
  • 13
-2

As for today 2021 Nov. 18th, there is no way provided by the Firebase SDK to fetch a user by email.

Salathiel Genese
  • 1,639
  • 2
  • 21
  • 37
  • 1
    The method from these answers still applies, however how you access it in v9 onwards has changed: [fetchSignInMethodsForEmail](https://firebase.google.com/docs/reference/js/auth.md#fetchsigninmethodsforemail) – samthecodingman Sep 18 '21 at 05:20
  • This API tells us which providers can a user signs-in with... Not the user account infos, like, for name one, his profile URL. – Salathiel Genese Sep 18 '21 at 05:31