Get other users firebase for admin user? - firebase

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:abc#gmail.com
|--UID2
|
|---Name:MyName
|---Surname:Mysurname
|---age:39
|---email:xyz.aaa#gmail.com
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 xyz.aaa#gmail.com , 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

Your DB structure seems good to fetch data.
There can be multiple approaches to search data.
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
You can use firebase API to fetch only the particular record with specified value like:
\
firebaseDB
.ref("Users")
.orderByChild("email")
.equalTo("xyz.aaa#gmail.com")
.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

Related

getting uid of users from firebase

I'm working on an android app in kotlin and i created a login and register with firebase authentication, now when i want to access the users info in the database using the uid i got with this FirebaseAuth.getInstance().currentUser?.uid.toString() i noticed that the current uid is deferent from the uid that the function createUserWithEmailAndPassword() gave the users when it created them and inserted the to the database.
my question is how do i get the original uid from createUserWithEmailAndPassword().
i was thinking of saving the uid in a global string in the function createUserWithEmailAndPassword() but I'm sure there's a better idea .

can i make a firebese model that has a user and the data of that user with authentication in that same document?

I am trying to make a db model on fire base but I didn't understand the user authentication quite yet in mongo db you can make a document that has all the user data like email for example and passwords in addition to that user data, but in firebase the user authentication is a separate data base , can i make users document that include users data and authentication with in the fire cloud db ? or at least can I connect AUTH and db together ?
Firebase Authentication handles everything from hashing passwords to storing credentials and you don't have to store anything in a database. It also generates a unique UID for each user that can be used to identify user specific data in a database.
For example, if you have a collection "posts" then you can store the author's UID along with post data.
Also check: Firestore data modeling with more than one collection for each user

Firebase: should I store user data on the authentication profile or database

I signed up a user in my Flutter app using FirebaseAuth.instance.verifyPhoneNumber(). This works perfectly and the user shows up in my Firebase Console.
FirebaseAuth.instance.currentUser() now returns the user for me whenever I open the app. Should I store user info (age, weight etc.) here, or should I create a users collection in my Database and link the currentUser().uid in there?
Also, should I store it on the Database linked against the uid, or linked against the login info. For example, link a user to the phoneNumber and not the uid. Because if you ever delete the user, but they want to register again, then they will get a new uid, but their phoneNumber / email will still stay the same and can therefore still link to their old data.
You can't add arbitrary data to the Firebase Authentication user profile. So you should store the additional data in a database, and indeed associate it with the UID of the user, so that you can easily look it up. This also allows you to implement searches for users more easily, as the client-side Firebase Authentication SDKs have no functionality to look up data for any user but the currently signed in one.
To the additional question: if a user deletes their account from your application, you should respect their wishes and delete the additional data that you store for them too.

Why use UID in Firebase? Should I use it

I know UID is used because it is unique. But in my app, all of them are registered with Google ID, Google ID is also unique. Should I use UID?
yes it is better to use the uid.
From the docs:
You can let your users authenticate with Firebase using their Google Accounts by integrating Google Sign-In into your app.
So after you authenticate the users, the uid will be in the authentication page in firebase. That id will help you later in the firebase database also and it is easier to use and add in the database.
Can easily be gotten using this:
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
Then you can use the method getUid() to get the userid. So using it will make the work easier for you.
From the docs:
After a user signs in for the first time, a new user account is created and linked to the credentials—that is, the user name and password, phone number, or auth provider information—the user signed in with. This new account is stored as part of your Firebase project, and can be used to identify a user across every app in your project, regardless of how the user signs in.
Also check this link: https://firebase.google.com/docs/auth/android/google-signin (Next Step section)
I'll suggest you use email ID instead of UID because if the user account is deleted from your Firebase Auth (either you delete it using Admin SDK, or perform a manual deletion on console), the next time user signs in with the same email ID will now give you a different UID and therefore all of your data in your database which rely on your UID won't be accessible.
However, you can't use use an email ID as it is, because Firebase key doesn't allow you to use . (dot) as keys, so just replace your . with a ,. You can find more information here.
TL;DR
Use email ID as it will always be unique unlike UID which gets generated every time a user signs in if that ID was previously deleted on Firebase Authentication server.

Get other's Firebase display name and photo URL from ID in Ionic3

I am using Firebase in my Ionic3 app. I am storing other user's ID as friend data. How can I get the Display name and photo URL for other users from their ID?
You can't get that from a client API and rightfully so as it would a security/privacy issue. You should use the Firebase Admin SDKs (eg: https://firebase.google.com/docs/auth/admin/manage-users#retrieve_user_data ), they allow you to lookup users by user ID. you get back a UserRecord which contains the displayName and photoURL along with all the user's data.

Resources