zelig74
zelig74

Reputation: 532

Is it possible to write complex object to Firebase authentication custom claims?

I am wondering if it is possible to write (and read back) a more complex object to Firebase's authentication custom claims. For instance:

admin.auth().setCustomUserClaims(uid, {groups: {groupId1: true, groupId2: true}, sections: {sectionId1: true}});

The documentation mentions only storing key - value pairs, not objects. The reason why I need it is that groupIds and sectionIds are auto-generated values, so I would prefere to not mix them together.

I would like to read this claims later in (for instance) Realtime database security rules like this:

{
  "rules": {
    "mydata": {
      "$groupId": {
        ".read":  "auth.token.sections.sectionId1 === true"
        ".write": "auth.token.groups[$groupId] === true",
      }
    }
  }
}

Is this allowed in Firebase Authentication?

Upvotes: 1

Views: 208

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317497

Yes, you can store any JSON object in custom claims, up to the size limit of 1000 bytes. It should be easy to try for yourself.

Upvotes: 2

Related Questions