Anonymous
Anonymous

Reputation: 4900

Firebase - userId keeps changing

In an Android project I use Firebase with signInAnonymously() and I am getting userId like this

userId = FirebaseAuth.getInstance().getCurrentUser().getUid()

and I use the userId to create children nodes in Firebase Realtime Database that only this user can access based on the access rules of that database.

The problem is that I noticed userId changes randomly and when that happens all content created by that user is lost to them. Is there something I can do to keep the same userId until the app is uninstalled? What other way can I use to ensure steady and exclusive access for that user to a Realtime database child? Can installation id be used?

Upvotes: 0

Views: 459

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

You can use Firebase Anonymous Authentication to create and use only temporary accounts to authenticate with Firebase. The Anonymous Authentication accounts don't persist across application uninstalls. When an application is uninstalled, everything that was saved locally will be wiped out, including the anonymous auth token that identifies that account. So there is no way you can reclaim that token for the user. So each time you sign in, a new UID is generated.

What other way can I use to ensure steady and exclusive access for that user to a Realtime Database child?

To always have the same UID, then you have to implement the Firebase Authentication with a provider like Google, Facebook, Twitter, and so on.

Upvotes: 1

Related Questions