Firebase: setting additional user properties
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.
Add Custom user Property through firebase function
I was able to do this by making a fetch request to the analytics endpoint by making a function like this. I know this is not recommended but it gets the job done.
let hitGA = () => {
const GA_TRACKING_ID = "your GA tracking ID"
const GA_CLIENT_ID = "steps to get this will be given in the resources below"
const event = {
category: "pricing",
action: "counted",
label: "num_active_users",
value: 71
}
let request = new XMLHttpRequest();
let message =
`v=1&tid=${GA_TRACKING_ID}&cid=${GA_CLIENT_ID}&aip=1&ds=add-on&t=event&ec=${event.category}&ea=${event.action}&el=${event.label}&ev=${event.value}`
request.open("POST", "https://www.google-analytics.com/collect", true);
request.send(message);
}
P.S. if you are curious to know how I stumbled into the solutiona and other references, read this post
Where to set User Properties in iOS for Firebase Analytics?
Go to UserProperty tab in Firebase
console of your project. Click NEW USER PROPERTY.
Enter a name and description for the user property, then click CREATE.
Let's suppose you have set USER_TYPE as Firebase
property .
Now you can fire this property using below code .
Analytics.setUserProperty(USER_TYPE, forName: "A/B/C")
It will take some time to reflect on your Events
tab of your Firebase
project console.
Adding Custom Attributes to Firebase Auth
You can't add custom attributes to Firebase Auth. Default attributes have been made available to facilitate access to user information, especially when using a provider (such as Facebook).
If you need to store more information about a user, use the Firebase realtime database. I recommend having a "Users" parent, that will hold all the User children. Also, have a userId key or an email key in order to identify the users and associate them with their respective accounts.
Hope this helps.
Adding new data to firebase users
There is no way to add arbitrary additional data to Firebase Authentication user profiles. If you want that, consider using the Firebase Realtime Database (or Cloud Firestore) for storing the additional information.
This approach has been covered in quite a few questions in the past, so I'll link you to those:
- Firebase: setting additional user properties
- Add extra User Information with firebase
- How do I link each user to their data in Firebase?
- Swift & Firebase - How to store more user data other than email and password?
- Store additional information during registration with Firebase in Android
- How to add additional information to firebase.auth()
Since a few weeks ago you can add small bits of information to the Firebase Authentication user profile. While this might sound like what you need, it is explicitly not meant for storing user metadata such as you need. Instead this is intended for storing so-called claims: properties about the user that you then access in the security rules. See the documentation for setting custom claims.
Related Topics
How to Use Underscore.Js as a Template Engine
Which Keycode for Escape Key with Jquery
How to Create Separate Angularjs Controller Files
How Do Browsers Pause/Change JavaScript When Tab or Window Is Not Active
iPad Web App: Detect Virtual Keyboard Using JavaScript in Safari
A Cors Post Request Works from Plain JavaScript, But Why Not with Jquery
How to Call Loading Function with React Useeffect Only Once
Rails - Could Not Find a JavaScript Runtime
How to Loop Through All Dom Elements on a Page
How to Find First Element of Array Matching a Boolean Condition in JavaScript
Selected Text Event Trigger in JavaScript
Benefits of Prototypal Inheritance Over Classical
How to Get Evaluated Attributes Inside a Custom Directive
How to Add an Object to an Array