8

In order to set a custom claim, one uses:

admin.auth().setCustomUserClaims(uid,{claim:value});

There does exist

admin.auth().updateUser(uid,{claim:value});

...but I'm not exactly clear on how the two are different, and neither one seems to get at actually removing a previously applied custom claim.

benomatis
  • 5,536
  • 7
  • 36
  • 59
ecalvo
  • 323
  • 2
  • 11

3 Answers3

17

From the documentation:

You can delete a user's custom claims by passing null for customClaims.

So this should delete the claim:

admin.auth().updateUser(uid, {claim: null});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    That was fast! Jen Person was right, you're the king of firebase stackoverflowage. Thanks! – ecalvo Jan 09 '18 at 19:52
  • 1
    You're welcome. In this case my work wasn't much more than opening the linked page about custom claims, search for `remove` (which gave no relevant hits) and then `delete`. – Frank van Puffelen Jan 09 '18 at 19:55
  • 4
    Using the latest admin SDK, I had to use `admin.auth().setCustomUserClaims(user.uid, null);` – TimL Apr 24 '20 at 09:28
  • @FrankvanPuffelen what if just one of many claims need to be removed? full read and then write? – Fakeer Jun 26 '20 at 03:34
  • Yup, there is no API to just send just the update, so read, modify in memory, write is the process. – Frank van Puffelen Jun 26 '20 at 04:27
9

@FrankvanPuffelen's answer no doubt was the correct one at the time he answered it, however, as it stands today (Nov 30, 2020) the 2nd parameter of the updateUser method, called properties, is an UpdateRequest interface that has no claim property.

Setting custom claims has now been moved to the setCustomUserClaims method.

You set them by doing...

admin.auth().setCustomUserClaims(uid, { admin: true });

...and the only way you can remove one is by setting the whole object to null. There seems to be no way to selectively remove one claim if there are multiple.

admin.auth().setCustomUserClaims(uid, null); 
benomatis
  • 5,536
  • 7
  • 36
  • 59
0

I don't think you can use updateUser for this, I think you still need to call

admin.auth().setCustomUserClaims(uid, {claim:null}); 
  • 3
    this sets claim attribute to null but doesn't seem to remove it – Fakeer Jun 26 '20 at 03:35
  • @Fakeer I think (though I only recently started using firebase) the old way was to use `updateUser` and the `claim` property appears to have been used to set custom claims, but that seems to have changed now and indeed, the new way is to use `setCustomUserClaims`, however, in latter case the `claim` property is not needed anymore, setting `claim: null` actually sets a custom claim called `claim` to have the value `null` instead. – benomatis Nov 30 '20 at 21:36