'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?



Solution 1:[1]

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)

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Haddad
Solution 2 iDecode