Tuesday 25 January 2022

Firebase Functions Claims setting one makes my other 'claims' undefined !

In my app we have 2 roles that we need to add to the ‘claims’ .

These are

  1. Admin

  2. Coordinator

Which in my Cloud Function I set the claim to either 'true' or 'false' . Which is 'Add' and 'Remove' respectively.


However I was noticing that when setting one it was making the other claim 'undefined' . Here's what one of my functions looked like. exports.addAdminRole = functions.https.onCall((data, context) => { 3 return admin.auth().getUserByEmail(data.email).then(user => { 4 return admin.auth().setCustomUserClaims(user.uid, { 5 admin: true, 7 }); 8 }).then(() => { 10 return { 11 message: `Success! ${data.email} has been made an admin` 12 } 13 }).catch(err => { 14 return err; 15 }) 16})

To fix this issue I had to pass though instructions on what the other Roles claim should be.
I'm using Vue.js by the way. So, the other role is set in the 'data'. Which I can pick up and pass it through like this
removeAdminRole({email: this.email, coordinator_role: this.coordinator_role}).then(() => {
this.successMessage = "The admin role has been removed !";
})
And then Cloud function will look like this.
exports.addAdminRole = functions.https.onCall((data, context) => {
return admin.auth().getUserByEmail(data.email).then(user => {
return admin.auth().setCustomUserClaims(user.uid, {
admin: true,
coordinator: data.coordinator_role
});
}).then(() => {
return {
message: `Success! ${data.email} has been made an admin`
}
}).catch(err => {
return err;
})
})


This now works as it should for me.

No comments: