2

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?

1 Answers1

5

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:

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Ahh thank you, I haven't read much on querying and I had this idea that I was going to have to write code that read manually read through all users. That makes much more sense. Now that I know more about what I needed to search for, I ran across this blog entry - https://www.firebase.com/blog/2013-10-01-queries-part-one.html#byemail – Matt Schmiermund Sep 24 '14 at 14:51
  • Yeah, that blog post is pretty much mandatory reading when you start working with Firebase. I updated my answer to include some code snippets. – Frank van Puffelen Sep 24 '14 at 14:53
  • I tried to use email as the name of the node, but it says it cannot support to store dots "." and all of the emails have them. How do you recommend to store it @FrankvanPuffelen? – Dx_ Aug 09 '16 at 16:57
  • Most developers replace the `.` with a `,`. But any escaping will likely do. – Frank van Puffelen Aug 09 '16 at 18:06