Home > OS >  Firebase - Should Anonymous UID get deleted after Existing user is Logged In?
Firebase - Should Anonymous UID get deleted after Existing user is Logged In?

Time:06-21

I have Anonymous enabled as one of my Sign-in providers so that a user can browse my app without actually needing to login.

Once an anonymous user is created, they get a random uid

enter image description here

If that user never created an account, when they do sign up via email or phone, that random uid becomes their actual uid, and instead of it saying (anonymous) it'll show their email or phone number.

But if the user had previously signed up, then signs out, when they open the app they'll have random uid. When they sign in with their actual email or phone, they'll end up using their actual uid, at which point the random uid will be useless. Should I delete that random uid from the database or keep it there? I haven't found anything that suggests what to do with anonymous uids that are no longer in use.

For example:

let anonymousUserUID = Auth.auth().currentUser?.uid // "abc..."
let anonymousUser = Auth.auth().currentUser

let credential = PhoneAuthProvider.provider().credential(withVerificationID: verificationID, verificationCode: verificationCode)
    
Auth.auth().signIn(with: credential, completion: { (authDataResult, error) in

   guard let userId = authDataResult?.user.uid else { return }

   let usersRef = Database.database().reference()
        .child("users")
        .child(userId)
   usersRef.observeSingleEvent(of: .value, with: { [weak self](snapshot) in

        if snapshot.exists() {
           // turns out this anonymous user is already registered and their uid is "xyz..."

           // is it a good practice to now delete the anonymousUser?
        }
    })
})

CodePudding user response:

As shown in the documentation on Firebase Authentication limits, each project can have up to 100 million anonymous accounts.

Leaving the UIDs for abandoned anonymous user accounts in the system is mostly harmless. But if you get millions of them or they get in the way for another reason, you can delete them.

  • Related