"deleteUser" Firebase cloud function: added deletion of user folder from Storage, added extra checks on IDs not being empty string

This commit is contained in:
gianantoniopini
2021-05-06 12:37:10 +02:00
parent 14ea8de709
commit 9f38ebd044

View File

@ -20,6 +20,10 @@ const deleteUserFunctionHandler = async (_, { auth }) => {
try { try {
const userId = auth.uid; const userId = auth.uid;
if (!userId) {
throw new Error("Could not retrieve the user's unique ID.");
}
const updates = {}; const updates = {};
const userResumesDataSnapshot = await admin const userResumesDataSnapshot = await admin
@ -31,7 +35,9 @@ const deleteUserFunctionHandler = async (_, { auth }) => {
const userResumes = userResumesDataSnapshot.val(); const userResumes = userResumesDataSnapshot.val();
if (userResumes) { if (userResumes) {
Object.keys(userResumes).forEach(async (resumeId) => { 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); await admin.database().ref().update(updates);
} }
const storageUserFolderPath = `users/${userId}/`;
await admin.storage().bucket().deleteFiles({
prefix: storageUserFolderPath,
});
return true; return true;
} catch (error) { } catch (error) {
throw new functions.https.HttpsError('internal', error.message); throw new functions.https.HttpsError('internal', error.message);