11

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.

Dan Levy
  • 3,931
  • 4
  • 28
  • 48
  • 1
    The question is a bit old and the answer has changed, people still come here for the right answer, would recommend if you could any of the other answers as correct. – frunkad May 20 '20 at 08:18

2 Answers2

16

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.

Fred Dupray
  • 233
  • 2
  • 6
  • 8
    Not correct, you can add 1000 bytes of custom data as claims to user info. see video linked on my answer. – Vitaliy A Apr 19 '19 at 15:07
  • 6
    where is your answer? – Aakash Goplani Oct 10 '20 at 18:26
  • 1
    FYI, @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. – broofa Apr 27 '21 at 12:00
9

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.
});
giraffesyo
  • 4,860
  • 1
  • 29
  • 39