diff --git a/backend/api/controllers/result.js b/backend/api/controllers/result.js index 395745e64..4582a1449 100644 --- a/backend/api/controllers/result.js +++ b/backend/api/controllers/result.js @@ -18,6 +18,16 @@ class ResultController { } } + static async deleteAll(req, res, next) { + try { + const { uid } = req.decodedToken; + await ResultDAO.deleteAll(uid); + return res.sendStatus(200); + } catch (e) { + next(e); + } + } + static async updateTags(req, res, next) { try { const { uid } = req.decodedToken; diff --git a/backend/api/controllers/user.js b/backend/api/controllers/user.js index 366500c51..31f28edce 100644 --- a/backend/api/controllers/user.js +++ b/backend/api/controllers/user.js @@ -24,7 +24,7 @@ class UserController { static async deleteUser(req, res, next) { try { - const { uid } = req.body; + const { uid } = req.decodedToken; await UsersDAO.deleteUser(uid); return res.sendStatus(200); } catch (e) { diff --git a/backend/api/routes/result.js b/backend/api/routes/result.js index 65e351e5d..bf2f261ab 100644 --- a/backend/api/routes/result.js +++ b/backend/api/routes/result.js @@ -10,4 +10,6 @@ router.post("/add", authenticateRequest, ResultController.addResult); router.post("/updateTags", authenticateRequest, ResultController.updateTags); +router.post("/deleteAll", authenticateRequest, ResultController.deleteAll); + module.exports = router; diff --git a/backend/api/routes/user.js b/backend/api/routes/user.js index 1369ecd62..c5ce077b1 100644 --- a/backend/api/routes/user.js +++ b/backend/api/routes/user.js @@ -10,7 +10,7 @@ router.post("/signup", UserController.createNewUser); router.post("/checkName", UserController.checkName); -router.post("/delete", UserController.deleteUser); +router.post("/delete", authenticateRequest, UserController.deleteUser); router.post("/updateName", authenticateRequest, UserController.updateName); diff --git a/backend/dao/result.js b/backend/dao/result.js index 19c415806..7211b5fb9 100644 --- a/backend/dao/result.js +++ b/backend/dao/result.js @@ -19,6 +19,10 @@ class ResultDAO { }; } + static async deleteAll(uid) { + return await mongoDB().collection("results").deleteMany({ uid }); + } + static async updateTags(uid, resultid, tags) { const result = await mongoDB() .collection("results") diff --git a/src/js/settings.js b/src/js/settings.js index 509458ff2..c5c01582c 100644 --- a/src/js/settings.js +++ b/src/js/settings.js @@ -791,6 +791,10 @@ $(".pageSettings #updateAccountPassword").on("click", (e) => { SimplePopups.list.updatePassword.show(); }); +$(".pageSettings #deleteAccount").on("click", (e) => { + SimplePopups.list.deleteAccount.show(); +}); + $(".pageSettings .section.customBackgroundSize .inputAndSave .save").on( "click", (e) => { diff --git a/src/js/simple-popups.js b/src/js/simple-popups.js index 4a98c1ddd..6198f39c9 100644 --- a/src/js/simple-popups.js +++ b/src/js/simple-popups.js @@ -269,6 +269,83 @@ list.updatePassword = new SimplePopup( } ); +list.deleteAccount = new SimplePopup( + "deleteAccount", + "text", + "Delete Account", + [ + { + placeholder: "Password", + type: "password", + initVal: "", + }, + ], + "This is the last time you can change your mind. After pressing the button everything is gone.", + "Update", + async (password) => { + // + try { + const user = firebase.auth().currentUser; + if (user.providerData[0].providerId === "password") { + const credential = firebase.auth.EmailAuthProvider.credential( + user.email, + password + ); + await user.reauthenticateWithCredential(credential); + } else if (user.providerData[0].providerId === "google.com") { + await user.reauthenticateWithPopup(AccountController.gmailProvider); + } + Loader.show(); + + Notifications.add("Deleting stats...", 0); + let response; + try { + response = await axiosInstance.post("/user/delete"); + } catch (e) { + Loader.hide(); + let msg = e?.response?.data?.message ?? e.message; + Notifications.add("Failed to delete user stats: " + msg, -1); + return; + } + if (response.status !== 200) { + throw response.data.message; + } + + Notifications.add("Deleting results...", 0); + try { + response = await axiosInstance.post("/results/deleteAll"); + } catch (e) { + Loader.hide(); + let msg = e?.response?.data?.message ?? e.message; + Notifications.add("Failed to delete user results: " + msg, -1); + return; + } + if (response.status !== 200) { + throw response.data.message; + } + + Notifications.add("Deleting login information...", 0); + await firebase.auth().currentUser.delete(); + + Notifications.add("Goodbye", 1, 5); + + setTimeout(() => { + location.reload(); + }, 3000); + } catch (e) { + Loader.hide(); + Notifications.add(e, -1); + } + }, + () => { + const user = firebase.auth().currentUser; + if (user.providerData[0].providerId === "google.com") { + eval(`this.inputs = []`); + eval(`this.buttonText = "Reauthenticate to delete"`); + } + } +); + list.clearTagPb = new SimplePopup( "clearTagPb", "text", diff --git a/static/index.html b/static/index.html index c1dbf400b..38e3fb306 100644 --- a/static/index.html +++ b/static/index.html @@ -3938,6 +3938,22 @@ +