15

I am building a firebase based android application which involves multiple user who authenticate and login to app. I want to provide a Unique to each users who authenticate&login and which will not be changed even if user reinstall the application so that I can use and rely on that key to make a separate json branch for that specific user in database based on that id.

I have searched the docs I found user.getUid() and user.getToken(). I am confused about these two which one will help me considering my situation.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
GeekWithGlasses
  • 572
  • 2
  • 12
  • 30
  • 1
    actually you are on the right way, for unique id you can use user.getUid() this id will always unique for every user as per their register credentials.Token is basically depends on device which will help you into send notification feature. – Rishabh Mahatha May 03 '17 at 09:33

1 Answers1

29

Definitely use user.getUid(). It's a standard Firebase practice to use this UID to create a dedicated JSON branch for that user (on the database), just like you described.

This UID will remain the same even if the user deletes your app. As soon as he logins back again, he will be assigned this exact same UID.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
  • 2
    Thank You Sir ^_^ that's what I just wanted to know. Small things worth sharing (: – GeekWithGlasses May 03 '17 at 09:47
  • this answers the question but there´s more to [consider](https://stackoverflow.com/questions/44540920/custom-firebase-user-identity-not-using-the-firebase-user-uid/44541182?noredirect=1#comment76073381_44541182) – Tord Larsen Jun 14 '17 at 11:00
  • @ErikHellberg Ok, but if you think that my answer helped you, please consider giving a +1 as well, besides leaving a comment. That way you signal fellow coders that this answer contains useful information. Thanks! – Paulo Mattos Jun 14 '17 at 11:41
  • 1
    What if it's an anonymous authentication, the UID will be different each time the App is reinstalled. Anyone knows a way to keep the same UID in this case? – nadine87 Dec 23 '18 at 11:01
  • 1
    @nadine87 I can't see why you need that. When performing a anonymous login, the user should know which if he uninstall the app, all data will be lost. Maybe you can work around it using something like SharedPreference to save the UID on localstorage, but I really don't know if you can login the user again with the same UID on firebase (that may cause effects like the user can't access something or other stuffs) [I'm learning firebase (actually it's my first day using firebase database) now, so i can't say for sure if it will work. Just try it, and then give me a feedback] – Israel Nascimento Jan 31 '19 at 19:55
  • 1
    @IsraelNascimento Yep, I think your assumption about *anonymous login* makes perfect sense: an anonymous login, by definition, *shouldn't* be able to persist state across sessions at all. By the way, glad to see a fellow *carioca* around here ;) – Paulo Mattos Jan 31 '19 at 20:27
  • @PauloMattos Good to see a fellow _carioca_ too, but sad for us (sad for you if you live in Rio like me, because it's really heat this summer. Otherwise, just sad for me haha) – Israel Nascimento Jan 31 '19 at 23:50
  • @IsraelNascimento So sad for both of us, haha! I live in Barra da Tijuca and even here is screaming hot these days :-( – Paulo Mattos Jan 31 '19 at 23:59
  • @PauloMattos Just think a little bit about my situation... I live in **Campo Grande** and I haven't air conditioner on my bedroom. It's like a beta test from hell xD – Israel Nascimento Feb 01 '19 at 00:13