I am trying to set a custom claim to an existing user in the emulator with the following code:
exports.addAdmin = functions.https.onRequest((req, res) => {
admin.auth().setCustomUserClaims("ZZyxFu17eN8y6orw0tSQ8Y0vyPFS", { admin: true })
});
The logs tell me the the execution of the function started. functions: Beginning execution of "addAdmin"
In the auth emulator I can see that the custom claim is successfully set to the user {"admin":true}
. But the execution of the function doesn't end.
When I shutdown the emulator with CTRL C I get the following logs
functions: Waiting for all functions to finish...
. Then after some minutes: functions: Functions emulator work queue did not empty before stopping ⚠ Your function was killed because it raised an unhandled error.
What am I doing wrong here?
CodePudding user response:
The setCustomUserClaims()
does end but you are not terminating the function itself. To terminate a HTTP function, you must return back a response. Try:
exports.addAdmin = functions.https.onRequest(async (req, res) => {
await admin.auth().setCustomUserClaims("ZZyxFu17eN8y6orw0tSQ8Y0vyPFS", { admin: true })
return res.json({ message: "Claims updated" })
});
Checkout the documentation on terminating Cloud Functions and this video.