Updated "deleteUser" Firebase cloud function to perform updates in an atomic way, tweaked UI call and error handling

This commit is contained in:
gianantoniopini
2021-04-23 15:16:37 +02:00
parent d4e7914a27
commit 54ddfe30d7
2 changed files with 15 additions and 17 deletions

View File

@ -19,8 +19,9 @@ const deleteUserFunctionHandler = async (_, { auth }) => {
}
try {
// 1. Delete user resumes
const userId = auth.uid;
const updates = {};
const userResumesDataSnapshot = await admin
.database()
.ref('resumes')
@ -30,18 +31,21 @@ const deleteUserFunctionHandler = async (_, { auth }) => {
const userResumes = userResumesDataSnapshot.val();
if (userResumes) {
Object.keys(userResumes).forEach(async (resumeId) => {
await admin.database().ref(`resumes/${resumeId}`).remove();
updates[`resumes/${resumeId}`] = null;
});
}
// 2. Delete user
const userDataSnapshot = await admin
.database()
.ref(`users/${userId}`)
.once('value');
const user = userDataSnapshot.val();
if (user) {
await admin.database().ref(`users/${userId}`).remove();
updates[`users/${userId}`] = null;
}
if (Object.keys(updates).length > 0) {
await admin.database().ref().update(updates);
}
return true;

View File

@ -35,7 +35,7 @@ const Settings = ({ id }) => {
dispatch({ type: 'change_language', payload: lang });
};
const handleDeleteAccount = () => {
const handleDeleteAccount = async () => {
if (deleteText === t('builder.settings.dangerZone.button')) {
setDeleteText(t('shared.buttons.confirmation'));
return;
@ -43,18 +43,12 @@ const Settings = ({ id }) => {
setDeleteText(t('shared.buttons.loading'));
setTimeout(
() =>
deleteAccount()
.then(() => {
setDeleteText('Buh bye! :(');
})
.catch((error) => {
toast.error(error.message);
try {
await deleteAccount();
} catch (error) {
toast.error('An error occurred deleting your account');
setDeleteText(t('builder.settings.dangerZone.button'));
}),
500,
);
}
};
return (