From 674316784ac9d98eba808507566b6e110584d9aa Mon Sep 17 00:00:00 2001 From: Miodec Date: Mon, 12 Jul 2021 23:12:03 +0100 Subject: [PATCH] converted email updating --- backend/api/controllers/user.js | 11 +++++++++++ backend/api/routes/user.js | 2 ++ backend/dao/user.js | 4 +++- src/js/simple-popups.js | 11 +++-------- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/backend/api/controllers/user.js b/backend/api/controllers/user.js index 210931fb4..9625e3c47 100644 --- a/backend/api/controllers/user.js +++ b/backend/api/controllers/user.js @@ -65,6 +65,17 @@ class UserController { } } + static async updateEmail(req, res, next) { + try { + const { uid } = req.decodedToken; + const { newEmail } = req.body; + await UsersDAO.updateEmail(uid, newEmail); + return res.sendStatus(200); + } catch (e) { + return next(e); + } + } + static async getUser(req, res, next) { try { const { uid } = req.decodedToken; diff --git a/backend/api/routes/user.js b/backend/api/routes/user.js index b35aef1c0..f8a4bee48 100644 --- a/backend/api/routes/user.js +++ b/backend/api/routes/user.js @@ -12,6 +12,8 @@ router.post("/delete", UserController.deleteUser); router.post("/updateName", authenticateRequest, UserController.updateName); +router.post("/updateEmail", authenticateRequest, UserController.updateEmail); + router.get("/", authenticateRequest, UserController.getUser); router.post("/tags/add", authenticateRequest, UserController.addTag); diff --git a/backend/dao/user.js b/backend/dao/user.js index d5365500b..b00ca4216 100644 --- a/backend/dao/user.js +++ b/backend/dao/user.js @@ -37,7 +37,9 @@ class UsersDAO { static async updateEmail(uid, email) { const user = await mongoDB().collection("users").findOne({ uid }); if (!user) throw new MonkeyError(404, "User not found"); - return await updateAuthEmail(uid, email); + await updateAuthEmail(uid, email); + await mongoDB().collection("users").updateOne({ uid }, { $set: { email } }); + return true; } static async getUser(uid) { diff --git a/src/js/simple-popups.js b/src/js/simple-popups.js index adf98367d..1b2b6a751 100644 --- a/src/js/simple-popups.js +++ b/src/js/simple-popups.js @@ -172,25 +172,20 @@ list.updateEmail = new SimplePopup( .reauthenticateWithCredential(credential) .then(() => { axiosInstance - .post("/updateEmail", { + .post("/user/updateEmail", { uid: user.uid, previousEmail: previousEmail, newEmail: newEmail, }) .then((data) => { Loader.hide(); - if (data.data.resultCode === 1) { + if (data.status === 200) { Notifications.add("Email updated", 0); setTimeout(() => { AccountController.signOut(); }, 1000); - } else if (data.data.resultCode === -1) { - Notifications.add("Current email doesn't match", 0); } else { - Notifications.add( - "Something went wrong: " + JSON.stringify(data.data), - -1 - ); + Notifications.add(data.message); } }); })