28

I'd like to add a property to a Firebase user object. The user documentation says that I can only store additional properties using the Firebase real time database.

I am unsure on how this can works in practice.

What does the following mean in practice?

You cannot add other properties to the Firebase User object directly; instead, you can store the additional properties in your Firebase Realtime Database.

I interpret it as following:

"you cannot modify properties of a FIRUser object but you can combine this with additional objects"

I found the set function documentation which I interpet in this way:

  var userRef = ref.child("users");
  userRef.set({
    newfield: "value"
  });

Is this a sensible approach?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
mm24
  • 9,280
  • 12
  • 75
  • 170

2 Answers2

18

You're almost there. In the legacy Firebase documentation, we had a section on storing such additional user data.

The key is to store the additional information under the user's uid:

    let newUser = [
        "provider": authData.provider,
        "displayName": authData.providerData["displayName"] as? NSString as? String
    ]
    // Create a child path with a key set to the uid underneath the "users" node
    // This creates a URL path like the following:
    //  - https://<YOUR-FIREBASE-APP>.firebaseio.com/users/<uid>
    ref.childByAppendingPath("users")
       .childByAppendingPath(authData.uid).setValue(newUser)

I've added a note that we should add this information in the new documentation too. We just need to find a good spot for it.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 7
    So the "users" node does not exist by default, unless created like this correct? I agree this should be in the new docs. Would have saved me 2 days of confusion. :) – cdock Sep 09 '16 at 17:37
  • 1
    Im really confused too, im trying to achieve the exact dame thing however bring in an additional field upon registration for a zip/post code and save it somewhere but the docs lead me to believe this isn't possible? – PhpDude Feb 19 '17 at 12:58
5

According to the Custom Claims documentation,

The Firebase Admin SDK supports defining custom attributes on user accounts. [...] User roles can be defined for the following common cases:

  • Add an additional identifier on a user. For example, a Firebase user could map to a different UID in another system.

[...] Custom claims payload must not exceed 1000 bytes.

However, do this only for authentication-related user data, not for general profile information, per the Best Practices:

Custom claims are only used to provide access control. They are not designed to store additional data (such as profile and other custom data). While this may seem like a convenient mechanism to do so, it is strongly discouraged as these claims are stored in the ID token and could cause performance issues because all authenticated requests always contain a Firebase ID token corresponding to the signed in user.

Use custom claims to store data for controlling user access only. All other data should be stored separately via the real-time database or other server side storage.

Community
  • 1
  • 1
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404