Manas Saxena
Manas Saxena

Reputation: 2365

Get other users firebase for admin user?

I am working on a registration and login functionality for my webapp.

All registered users have an email and UID in firebase auth db. The UID is used as key in firebase realtime db. The following json structure in firebase realtime db

users
|
|--UID1
   |
   |---Name:Amanda
   |---Surname:Waller
   |---age:30
   |---email:[email protected]
|--UID2
   |
   |---Name:MyName
   |---Surname:Mysurname
   |---age:39
   |---email:[email protected]

Now I have an admin user who needs to access other users data , for example the admin wants to search and retrieve the user with email [email protected] , but the JSON key i have chosen is UID , is there a way for an admin user to get UID based on email id of user?Or should I redesign my db? Using email as key doesn't really seem straight forward given that email can contain special characters as stated in the answer to this question:

Referencing authentication db data in real time db firebase

Upvotes: 0

Views: 764

Answers (1)

Mohammed Rampurawala
Mohammed Rampurawala

Reputation: 3112

Your DB structure seems good to fetch data.

There can be multiple approaches to search data.

  1. Download all the users and save it in memory and when admin searches the email id then you can search on local storage and get uID.

Don't forget to add a childListener when new user is added so you have to update the local user list

  1. You can use firebase API to fetch only the particular record with specified value like:

\

firebaseDB
            .ref("Users")
            .orderByChild("email")
            .equalTo("[email protected]")
            .once('value')
            .then((snapshot) => {
                //Received Snapshot
            })
            .catch(er => console.log(JSON.stringify(er)));

In 2nd Approach you will need to index the email value in firebase database rules and need to search only when admin has entered full email id.

Thanks

Upvotes: 2

Related Questions