Adding Custom Attributes to Firebase Auth
Solution 1
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.
Solution 2
While in most cases you cannot add custom information to a user, there are cases where you can.
If you are creating or modifying users using the Admin SDK, you may create custom claims. These custom claims can be used within your client by accessing attributes of the claims object.
Swift code from the Firebase documentation:
user.getIDTokenResult(completion: { (result, error) in
guard let admin = result?.claims?["admin"] as? NSNumber else {
// Show regular user UI.
showRegularUI()
return
}
if admin.boolValue {
// Show admin UI.
showAdminUI()
} else {
// Show regular user UI.
showRegularUI()
}
})
Node.js code for adding the claim:
// Set admin privilege on the user corresponding to uid.
admin.auth().setCustomUserClaims(uid, {admin: true}).then(() => {
// The new custom claims will propagate to the user's ID token the
// next time a new one is issued.
});
Dan Levy
Updated on July 09, 2022Comments
-
Dan Levy almost 2 years
I have hunted through Firebase's docs and can't seem to find a way to add custom attributes to FIRAuth. I am migrating an app from Parse-Server and I know that I could set a user's username, email, and objectId. No I see that I have the option for email, displayName, and photoURL. I want to be able to add custom attributes like the user's name. For example, I can use:
let user = FIRAuth.auth()?.currentUser if let user = user { let changeRequest = user.profileChangeRequest() changeRequest.displayName = "Jane Q. User" changeRequest.photoURL = NSURL(string: "https://example.com/jane-q-user/profile.jpg") changeRequest.setValue("Test1Name", forKey: "usersName") changeRequest.commitChangesWithCompletion { error in if error != nil { print("\(error!.code): \(error!.localizedDescription)") } else { print("User's Display Name: \(user.displayName!)") print("User's Name: \(user.valueForKey("name"))") } } }
When I run the code, I get an error that "usersName" is not key value compliant. Is this not the right code to use. I can't seem to find another way.
-
Vitaliy A about 5 yearsNot correct, you can add 1000 bytes of custom data as claims to user info. see video linked on my answer.
-
Aakash Goplani over 3 yearswhere is your answer?
-
broofa about 3 yearsFYI, @VitaliyA's answer has been deleted. 'Not entirely sure why. Either because it was just a link to a youtube video, or because "claims" are not settable by clients. They're part of Firebase's server-only
admin
API.