Monday, May 20, 2024
182
rated 0 times [  185] [ 3]  / answers: 1 / hits: 27260  / 8 Years ago, tue, may 24, 2016, 12:00:00

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?



 Answers
71

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.


[#62047] Saturday, May 21, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gideons

Total Points: 197
Total Questions: 106
Total Answers: 108

Location: Moldova
Member since Sat, Jan 29, 2022
2 Years ago
;