Search code examples
typescriptfirebasefirebase-authenticationgoogle-cloud-functionsfirebase-admin

How can I access firebase custom claims from a cloud function


EDIT: I can do it as below but I would prefer a nicer solution:

console.log((operatorUser.customClaims as any)['admin']);
console.log((operatorUser.customClaims as any).admin);

I am trying to check a users custom claim in a callable cloud function.

I am following the example from here: https://firebase.google.com/docs/auth/admin/custom-claims#set_and_validate_custom_user_claims_via_the_admin_sdk

It has the error below when trying to access the custom claims:

"Element implicitly has an 'any' type because expression of type '"admin"' can't be used to index type 'Object'. Property 'admin' does not exist on type 'Object'"

 const operatorUser = await admin.auth().getUser(operatorUserDoc.id);
 console.log(operatorUser.customClaims['admin']);

Solution

  • Type signature of customClaims was improved recently. If you're using the latest version of the SDK you should be able to just do user['key'] to access claims.

    See https://github.com/firebase/firebase-admin-node/issues/864 for more context.