Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase: setting additional user properties

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?

like image 220
mm24 Avatar asked May 24 '16 13:05

mm24


People also ask

What is Firebase user properties?

User properties are attributes you define to describe segments of your user base, such as language preference or geographic location. These can be used to define audiences for your app.

Can you add users to Firebase?

Adding members to your Firebase project allows you to collaborate across your team. You can assign each member a role based on the level of access the member needs for your project. Project members are not limited to just individual users: domains, groups, and service accounts can all be members of a Firebase project.

How does Firebase identify users?

Auth tokensCreated by Firebase when a user signs in to an app. These tokens are signed JWTs that securely identify a user in a Firebase project. These tokens contain basic profile information for a user, including the user's ID string, which is unique to the Firebase project.

Can you change user UID Firebase?

uid cannot be changed. You can create your own custom uid for users. You will need a users table which uses your custom uid rather than the one created by Firebase.


2 Answers

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.

like image 92
Frank van Puffelen Avatar answered Oct 18 '22 17:10

Frank van Puffelen


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.

like image 42
Dan Dascalescu Avatar answered Oct 18 '22 15:10

Dan Dascalescu