I'm building fairly lengthy firebase cloud functions, and to make the code more readable, I'm splitting some verifications into different files.
For instance, when a user wants to make a change, I check whether their account status is set to activated. I offload that logic into a separate authFunctions.js file, and if the user does not pass, I throw an error directly from within the authfunctions.js file.
I've just noticed however that throwing the error from this imported file doesn't stop the calling function from proceeding, and hence a user is able to bypass the security.
index.js
exports.setDraftOrganisation = functions.https.onCall(async (data, context) => {
// Check if user's account is not disabled (or pending info confirmation)
authFunctions.validateUserAccountIsActive()
// Proceed with operation
updateData()
})
authFunctions.js:
exports.validateUserAccountIsActive = async function() {
if(verificationFailed) {
const functions = require('firebase-functions')
functions.logger.error(`User's account is not activated`)
throw new functions.https.HttpsError('failed-precondition')
}
}
Is there any elegant way to not halt the main function if the imported function signals a blocking issue? I was thinking of maybe using try {} catch(){} in the main function, but that would add a lot more lines of code in the main function effectively defeating the purpose of moving the logic into authFunctions.js.