EdgeDev
EdgeDev

Reputation: 2486

Is it reliable to use email address as primary key/uid on Firebase Realtime Database?

Firebase real-time database has Character Set Limitations that prevent these characters

. (period)
$ (dollar sign)
[ (left square bracket)
] (right square bracket)
# (hash or pound sign)
/ (forward slash)

from being present in a primary key.

But this library for firebase real-time database on google apps script uses escaped email as auth.uid

Therefore, I structured my FB real-time DB as below:

{  
   users={  
      auth.uid={  //auth.uid == escaped email
         phone_no=+123456789012,
         [email protected]
      },
      ... //rest of the array
   }
}

My concern is that, the possibility for two different users to have the same escaped email.

For example:

User1 email: [email protected]

User1 escaped email: johndoefoobarcom

User2 email: [email protected]

User2 escaped email: johndoefoobarcom

Do email services especially Gmail allow users to register emails that are similar as shown above?

Here's what my rules look like

{
  "rules": {
    "users": {
      "$user_id": {
        ".write": "$user_id === auth.uid",
        ".read": "$user_id === auth.uid"
      }
    }
  }
}

So, you can see why i am using the escaped email (the auth.uid) as rootkey (because the auth.uid is already the escaped email address)

Here is Apps Script Code -> https://codepen.io/edge-developer/pen/JVPNOY

Upvotes: 0

Views: 1238

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80924

You are using a very old version of firebase, you need to upgrade to firebase js 5.8.5.

Also no it is better to use the userId that you obtain are you authenticate a specific user. The main reason to use the uid is because it is unique for each user.

Check here :

Why use UID in Firebase? Should I use it

Upvotes: 3

Related Questions