Reputation: 2365
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
Reputation: 3112
Your DB structure seems good to fetch data.
There can be multiple approaches to search data.
Don't forget to add a childListener
when new user is added so you have to update the local user list
\
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