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?
- 78,874
- 25
- 140
- 134
- 2,480
- 2
- 23
- 67
-
7Always use an ID under your control. Using someone else's builds a dependency into your system, one which can have a severe and nasty impact if the other system changes its concept of ID or if it stops working. – High Performance Mark Jan 22 '18 at 10:55
-
@Julidi are you authenticating your users? – Peter Haddad Jan 22 '18 at 11:06
-
@Peter Haddad yah, of course – dontknowhy Jan 22 '18 at 11:07
-
@High Performance Mark Sorry, so you mean, do I have to use UID in firebase? or I should create a new ID for me that`s not UID which provides from Firebase. Google ID is a nice unique key, is not it? Google ID is unique as long as the system will change later and Google will not crash. – dontknowhy Jan 22 '18 at 11:09
2 Answers
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)
- 78,874
- 25
- 140
- 134
-
Lastly, one more question, I know that this Firebase user system is helpful in many ways, but I wonder if there is a disadvantage in using the root key of each user in my Firebase Database as Google ID instead of 'UID'. Do you think that 'UID' should also be stored in the database? – dontknowhy Jan 22 '18 at 11:29
-
yes if you are using firebase-database also then it is probably better to use `uid` (use stuff that they offer, just to make the job easier honestly) – Peter Haddad Jan 22 '18 at 11:31
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.
- 22,623
- 19
- 99
- 186
-
This is not good advice. Google says, "Applications should not key users by email address since a Google account's email address can change. Use [id] as a key instead." – Jay Dec 11 '22 at 05:18
-
@Jay Reference please? And which `id` are you talking about, `UID`? As I mentioned in the answer, it can change, so how will you fix that? – iDecode Dec 11 '22 at 07:43
-
Couldn't find the documentation reference, but you can check the package code in https://github.com/flutter/plugins/blob/main/packages/google_sign_in/google_sign_in/lib/src/common.dart , where the description of the field mentions what I quoted in my previous comment. And, no, not UID, but they recommend using Google ID, which is available in 'id', along with email, photourl and displayname. – Jay Dec 11 '22 at 07:59
-
@Jay I doubt if this `id` is available for users who signed in with 'email address'. – iDecode Dec 11 '22 at 08:29