getting uid of users from firebase - 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 .

Related

How to delete firebase anonymous UID after login with Credential?

My app makes the user create a new anonymous ID automatically when downloading the app and when the user logs in, for example, by Facebook, the app change UID.
The problem is when I call FirebaseAuth.instance.signInWithCredential, It creates a new UID or change to UID that links to this credential, and the anonymous ID is never deleted. If many users relogin this app, many unuse anonymous ID and data will be garbage in firebase.
I have an idea to store UID in a variable, and when sign-in is successful, I delete using that UID, but firebase allows delete UID only current account. How can I solve this?
It sounds like you want to allow a user that you signed in anonymously to upgrade to an identified account. The idiomatic way to do that is to link the Facebook account to the existing anonymous account, so that the UID remains the same. To do this, follow the process described in the documentation on account linking and in the FlutterFire documentation on linking user accounts.

is UID in firebase automatically created/read for each user?

I'm sorry if my question is basic question...
is UID in firebase automatically created when we implement .setUid() at the first user sign-up account? and if we are login to that user account and implement .getUid(), can we retrieve the same UID?
I use Android Studio and Kotlin as programming language
No matter how you implement authentication on your app, firebase will automatically set a uid for each account created. If you go to the auth section of your firebase console and manually add an account, you will see on the right hand side a uid generated for that account.
To reference this uid within your code the uid does not need to be stored in any database solution for the uid is attatched to the account within FirebaseAuth..
To pull the uid for reference once the user has signed in, first you need to set the auth global variable..
private lateinit var auth: FirebaseAuth
Then anywhere within the scope of your functions you can set your uid reference variable like this..
val myUid = FirebaseAuth.getInstance().currentUser.uid;

Creating User with Email, password, display name and photoURL

I'm creating an application with flutter and using firebase for authentication in data storage (possible more).
I have an auth onCreate cloud function which adds some user information to my realtime database whenever a user is created. But, I also want to set a displayName and photoURL when the user is created because if I update the profile info after, the onCreate won't be triggered. I also don't want users to be able to write to this part of the database.
Is there any method I can user to pass an displayName and photoURL when the user is created? Currently, I'm just creating the user with an email and password.
Thanks.
Instead of creating the users directly from the client, you can use a callable function to create users using the Admin SDK, which includes displayName and photoURL in its options.

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.

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: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

Resources