24

I'm building an app with Firebase Real-Time Database. The app will be mainly offline at first, but we are planning an online update later. I'm planning to use the anonymous sign in, to get an ID for the user and store all his data under this ID:

Auth.auth().signInAnonymously(completion: { (user, error) -> Void })

After signing in I get an ID that looks like something like this: pCfgFOvEYEYvfWHaaaaavKgs8h33

Is it guaranteed that this ID will ALWAYS stay unchanged on a given device? I couldn't find any documentation about this.

Wiingaard
  • 4,150
  • 4
  • 35
  • 67
  • 2
    You will get a random unique `uid` per device. It shouldn't change unless signed out as Frank answered below. – bojeil Dec 18 '17 at 07:56
  • 2
    Does the `uid` remain the same if you factory reset a device and re-install the app? – Taku Oct 04 '19 at 04:29

7 Answers7

28

A user's UID never changes. So after you sign in a user anonymously, the UID will remain the same (even when you call signInAnonymously again), until you call signout, or until the user uninstalls the app.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 2
    Awesome, I'll count on that then :) According to the documentation of `signInAnonymously `: "If there is already an anonymous user signed in, that user will be returned instead.", so It seems that it can be called multiple times without generating a new ID.. – Wiingaard Dec 18 '17 at 10:48
  • Oh? That's new to me. Many developers have struggled with that in the past, so it would be a welcome change. – Frank van Puffelen Dec 18 '17 at 15:07
  • 13
    What if a user uninstalls the app and reinstalls it again? Would the UID change then? – dshukertjr Jan 05 '18 at 15:17
  • @FrankvanPuffelen Is there a way to keep the same UID between signing in/out with `signInAnonymously`? We track some user data and we would really really like to keep it linked to the uid rather than device id – Riley Fitzpatrick Apr 15 '20 at 01:02
  • Once the UID is gone, it can't be recovered. You'll want to implement your own custom sign-in in that case. But the thing is: you'll need to base it on the device ID to have it survive reinstalls. – Frank van Puffelen Apr 15 '20 at 01:30
  • 2
    Sorry to post on something so old, but, what if you link the anon account to something else (like google play or game center). will the userId stay the same / is there a way to keep the anon one? – Jeff Feb 11 '22 at 00:33
  • 3
    Yes, when you link an account **to** the anonymous account, the merged account retains the UID of that anonymous account. This is in fact a primary use-case for account linking, so I recommend giving it a try. – Frank van Puffelen Feb 11 '22 at 00:41
  • 1
    Well I have found out that uninstalling and reinstalling on ios gives me the same uid. (Perheps it may change but when i tried id didn't) Doing the same on android always gives me a new uid. This will force me to add login to my app where I thought I could stick with anonymous login. I can't have it that the users loose all their data due to e reinstall. – Gunnar Eketrapp Sep 05 '22 at 06:25
  • actually, once you log out and try login in again with an anonymous sign in you will get a different ID each time. – Saurabh Kumar Oct 06 '22 at 19:01
13

Here's what I've found using Flutter:

  • if you call FirebaseAuth.signInAnonymously() twice without calling FirebaseAuth.signOut() in between, you get the same uid (from AuthResult.user.uid) as the first call. This is true even if you close the app and call FirebaseAuth.signInAnonymously()
  • if you call FirebaseAuth.signOut() and then call FirebaseAuth.signInAnonymously(), you get a new uid
  • if you uninstall the app and call FirebaseAuth.signInAnonymously(), you get a different uid from the previous install

Is summary, you get a new uid if you call signInAnonymously() after signOut() or after uninstalling the app.

user2233706
  • 6,148
  • 5
  • 44
  • 86
4

You should check this out:

Firebase also allows for anonymous auth sessions, which are typically used to persist small amounts of data while waiting for a client to authenticate with a permanent auth method. These anonymous sessions can be configured to last days, weeks, months, even years… until the user logs in with a permanent login method or clears her browser cache. Web apps often use local datastores like sessionStorage or localStorage to accomplish similar tasks.

It is not guaranteed that the ID will always stay unchanged on a given device. Firebase stores the anonymous auth sessions to browser's localStorage. If users open your application in incognito mode / private browsing mode, another browsers, or they clear their browser's localStorage, firebase will issue another user ID.

Ben
  • 5,069
  • 4
  • 18
  • 26
2

If you login anonymous then your UID will remain unchanged until you logout. Once you logged out, and log in again, you will get a new UID. Or even when the user uninstalls the app, you will get UID again.

Ganesa Vijayakumar
  • 2,422
  • 5
  • 28
  • 41
Rahul
  • 474
  • 5
  • 8
2

Is this answer still valid? I have started to get new UID's each time on an android phone.

This can be related to that android device id. I.e. I am using flutter's device_ino_plus package and yesterday I had to do a fix related to that androiceId has started to become null.

    if (Platform.isAndroid) {
    // this returns null for now.
    // See https://github.com/fluttercommunity/plus_plugins/issues/939
    //
    // final AndroidDeviceInfo androidInfo = await deviceInfoPlugin.androidInfo;
    // return androidInfo.androidId!;

    // UUID workaround
    ...
Gunnar Eketrapp
  • 2,009
  • 1
  • 19
  • 33
1

Firebase UID will change on signout, on clearing the App data from settings and on re-installing the app after a uninstall.

Rahul Rastogi
  • 4,486
  • 5
  • 32
  • 51
0

I had an issue where the user id was changing whenever I close and re-open the app. deleting the app and re-installing it fixed the issue.

leo
  • 113
  • 3
  • 11