Firebase: setting additional user properties

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.


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.