How to set custom claim to already existing claim without overriding?

Hello,

I wish to have multiple individual stages (actions) in the login action pipeline. Each individual action will need to add stuff to the same claim object.

As a more properly explained example. I want my auth login flow to have multiple stages like so

[Action1]
     |
[Action 2]
     |
[Action 3]

The made up code for Action 1

exports.onExecutePostLogin = async (event, api) => {
  if (event.authorization) {
    api.idToken.setCustomClaim("http://foo-bar/my-claim", {
        "x-user-role": generateUserRole(),
    })
  }
};

Then the next Action in the pipeline, Action 2

exports.onExecutePostLogin = async (event, api) => {
  if (event.authorization) {
    api.idToken.setCustomClaim("http://foo-bar/my-claim", {
        "x-foo-bar": "baz",
    })
  }
};

Then the next Action in the pipeline, Action 3

exports.onExecutePostLogin = async (event, api) => {
  if (event.authorization) {
    api.idToken.setCustomClaim("http://foo-bar/my-claim", {
        "something-else": "goes here",
    })
  }
};

The problem seems to be that each action uses the setCustomClaim method which seems to overwrite the object at the claim location “http://foo-bar/my-claim”, whereas I wish to add new object keys each time instead. I wish the final claim on the id token after all 3 stages to look like the following:

{
    "http://foo-bar/my-claim": {
        "x-user-role":  "some-role",
        "x-foo-bar": "baz",
        "something-else": "goes here",
    },
    ....
}

Hello,

I think the best way is to define an empty array is at the start of the Action script. Then add the items to that array along the way. Finally, use addToCustomClaim once to add all items at once.

Something like this should work:

exports.onExecutePostLogin = async (event, api) => {
    const claimValue = [];
    if (true) {
        claimValue.push(`item1`);
    }
    if (true) {
       claimValue.push(`item2`);
    }
    api.idToken.addToCustomClaim("http://foo-bar/my-claim", {
        "x-some-new-key": claimValue.toString()
    })
    // result http://foo-bar/my-claim: {"x-some-new-key": "item1,item2"}
}

Please let me know if the above approach suits you.

Thanks,
Art

Hi art.stuples,

Now that I see your answer I see that I may have explained my problem poorly. I re-wrote my original question to better articulate my problem.

Thanks!