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

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;

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 .

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.

Firebase cookies without user login [duplicate]

I use Flutter and Firebase, just ask myself how to allow users to add items to cart when they're not logged in yet and keep the cart when they logging in, anyone have an idea for this ?
On Firebase you'd typically start the user off with an anonymous authentication account. With that sort of account, Firebase generates a user ID (UID) for the user, without them having to enter any credentials. You then associate the cart/items with the UID, and then when you/they are ready to identify themselves, you can sign them in with another provider, and link that to the anonymous account.
I've provided links to the documentation for Android developers above, but the same functionality is available in the FlutterFire libraries too. For example, anonymous sign-in on Flutter is as simple as:
UserCredential userCredential = await FirebaseAuth.instance.signInAnonymously();
you can store this data in a separate database SQL lite as an example

What uid is best to use for Firebase Firestore user document ids?

I have a firebase project for which I have created a firebase firestore database. I use firebase auth for signing users in.
I want all documents in reference with a specific user to have their id equal to the users uid.
At first, I set the document id to the FirebaseAuth.FirebaseUser.uid
The problem with that is that the FirebaseUser.uid can change under certain circumstances (E.g. the user changes his Google account password)
Is there a persistent uid I can use to refer to a specific user no matter the authentication provider ? If not, what would be best practice ?
Normally, for a given user in Firebase Auth, the user unique ID (the uid generated by the Auth service) will never change.
For example, you can, with the Admin SDK, change the email of a user without changing its uid, see this doc.
You can also have several identity providers for the same account, with the same uid, see here:
You can sign in users to your apps using several methods: email
address and password, federated identity providers, and your custom
auth system. You can associate more than one sign-in method with a
user: for example, a user can sign in to the same account using an
email address and a password, or using Google Sign-In
Note that changing the password of an identity provider account (e.g. "changing the Google account password" as you mentioned) has no impact at all on the uid or on the other user account elements (actually the identity provider password is not stored in Firebase Auth).
So, in conclusion, using the uid generated by the Auth service is the best solution for your requirement.

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.

Resources