vesii
vesii

Reputation: 3128

Using getUID or getEmail in order to keep to users in the firebase

I'm trying to figure out what is the proper way to keep the users in the firebase. Each user in my app must use an email. I saw people use:

new_user.put("email", email);
FirebaseUser user = mAuth.getCurrentUser();
if (user != null && user.getEmail() != null) {
    fireDB.collection("users").document(user.getEmail()).set(new_user);
}

On the other hand I saw people use:

fireDB.collection("users").document(user.getUID()).set(new_user);

Which is better? getUID() or getEmail()? Why?

Upvotes: 0

Views: 51

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317402

Don't use the email. Use the UID. It will never change, it's properly case sensitive, it doesn't contain any information (so it's safe to share with other users), and is always going to be a valid key for Firebase API calls.

Bear in mind also that a user isn't guaranteed to have an email address. Especially if they signed in with phone authentication.

If that's not enough reason, all of the provided sample code in the documentation uses the UID as the index. It's the gold standard for use with security rules, and recommended throughout the documentation:

And I discuss it in my blogs:

Upvotes: 1

Related Questions