From 9f38ebd0448e84b59118f6fe99f44b07fd452a7b Mon Sep 17 00:00:00 2001 From: gianantoniopini <63844628+gianantoniopini@users.noreply.github.com> Date: Thu, 6 May 2021 12:37:10 +0200 Subject: [PATCH] "deleteUser" Firebase cloud function: added deletion of user folder from Storage, added extra checks on IDs not being empty string --- functions/index.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/functions/index.js b/functions/index.js index 0a5319fd..9616e946 100644 --- a/functions/index.js +++ b/functions/index.js @@ -20,6 +20,10 @@ const deleteUserFunctionHandler = async (_, { auth }) => { try { const userId = auth.uid; + if (!userId) { + throw new Error("Could not retrieve the user's unique ID."); + } + const updates = {}; const userResumesDataSnapshot = await admin @@ -31,7 +35,9 @@ const deleteUserFunctionHandler = async (_, { auth }) => { const userResumes = userResumesDataSnapshot.val(); if (userResumes) { Object.keys(userResumes).forEach(async (resumeId) => { - updates[`resumes/${resumeId}`] = null; + if (resumeId) { + updates[`resumes/${resumeId}`] = null; + } }); } @@ -48,6 +54,11 @@ const deleteUserFunctionHandler = async (_, { auth }) => { await admin.database().ref().update(updates); } + const storageUserFolderPath = `users/${userId}/`; + await admin.storage().bucket().deleteFiles({ + prefix: storageUserFolderPath, + }); + return true; } catch (error) { throw new functions.https.HttpsError('internal', error.message);