Search code examples
javascriptfirebasefirebasesimplelogin

Firebase Authentication Service - lookup uid from email without login


I am building an app using Firebase, Firebase's Authentication Service (simple email/password login), and storing user information in a manner pretty similar to what is described here.

Part of my functionality will need to allow looking up another user based on an email address.

Is there a straightforward way to use an email to lookup the Firebase defined uid (i.e. 'simpleLogin:9') without having to iterate over all users, or being able to actually log them in?


Solution

  • Your users node is just a regular node in Firebase. So the only way to look up items is either by the name of the node or by the priority of the node.

    If you want to stick to using the uid as the node name, you can set the email address as the priority using setPriority or setWithPriority and then filter using startAt and endAt.

      // save new user's profile into Firebase so we can
      // list users, use them in security rules, and show profiles
      myRef.child('users').child(user.uid).setWithPriority({
        displayName: user.displayName,
        provider: user.provider,
        provider_id: user.id
      }, user.email);
    
      var userRef = myRef.child('users').startAt(user.email).endAt(user.email);
    

    But if you're only using simple email, you might consider simply storing the users by their email address to begin with:

      myRef.child('users').child(user.email).set({
        displayName: user.displayName,
        provider: user.provider,
        provider_id: user.id
      });
    
      var userRef = myRef.child('users').child(user.email);
    

    See also: