From 6d99002fd172875949579ed0dbc727896c8f1cf5 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Sat, 17 Aug 2024 20:23:34 -0700 Subject: [PATCH] Finish refactoring auth slice, fixed several bugs --- Client/src/Features/Auth/authSlice.js | 26 +++---------- Client/src/Utils/axiosConfig.js | 47 +++++++++++++++++++++++ Server/controllers/authController.js | 3 ++ Server/db/mongo/modules/monitorModule.js | 2 +- Server/db/mongo/modules/recoveryModule.js | 1 + Server/index.js | 2 +- 6 files changed, 59 insertions(+), 22 deletions(-) diff --git a/Client/src/Features/Auth/authSlice.js b/Client/src/Features/Auth/authSlice.js index e51d427ab..070cc3eb3 100644 --- a/Client/src/Features/Auth/authSlice.js +++ b/Client/src/Features/Auth/authSlice.js @@ -51,9 +51,6 @@ export const update = createAsyncThunk( const { authToken: token, localData: form } = data; const user = jwtDecode(token); try { - //1.5s delay to show loading spinner - await new Promise((resolve) => setTimeout(resolve, 1500)); - const fd = new FormData(); form.firstName && fd.append("firstName", form.firstName); form.lastName && fd.append("lastName", form.lastName); @@ -69,12 +66,8 @@ export const update = createAsyncThunk( form.deleteProfileImage && fd.append("deleteProfileImage", form.deleteProfileImage); - const res = await axiosInstance.put(`/auth/user/${user._id}`, fd, { - headers: { - Authorization: `Bearer ${token}`, - "Content-Type": "multipart/form-data", - }, - }); + const res = await axiosInstance.updateUser(token, user._id, fd); + return res.data; } catch (error) { if (error.response && error.response.data) { @@ -95,9 +88,7 @@ export const deleteUser = createAsyncThunk( const user = jwtDecode(data); try { - const res = await axiosInstance.delete(`/auth/user/${user._id}`, { - headers: { Authorization: `Bearer ${data}` }, - }); + const res = await axiosInstance.deleteUser(data, user._id); return res.data; } catch (error) { if (error.response && error.response.data) { @@ -116,7 +107,7 @@ export const forgotPassword = createAsyncThunk( "auth/forgotPassword", async (form, thunkApi) => { try { - const res = await axiosInstance.post("/auth/recovery/request", form); + const res = await axiosInstance.forgotPassword(form); return res.data; } catch (error) { if (error.response.data) { @@ -136,13 +127,8 @@ export const setNewPassword = createAsyncThunk( async (data, thunkApi) => { const { token, form } = data; try { - await axiosInstance.post("/auth/recovery/validate", { - recoveryToken: token, - }); - const res = await axiosInstance.post("/auth/recovery/reset", { - ...form, - recoveryToken: token, - }); + await axiosInstance.validateRecoveryToken(token); + const res = await axiosInstance.setNewPassword(token, form); return res.data; } catch (error) { if (error.response.data) { diff --git a/Client/src/Utils/axiosConfig.js b/Client/src/Utils/axiosConfig.js index 518af41e1..093210f90 100644 --- a/Client/src/Utils/axiosConfig.js +++ b/Client/src/Utils/axiosConfig.js @@ -104,4 +104,51 @@ axiosInstance.loginUser = async (form) => { return axiosInstance.post(`/auth/login`, form); }; +// ********************************** +// Update in an exisiting user +// ********************************** +axiosInstance.updateUser = async (authToken, userId, form) => { + return axiosInstance.put(`/auth/user/${userId}`, form, { + headers: { + Authorization: `Bearer ${authToken}`, + "Content-Type": "application/json", + }, + }); +}; + +// ********************************** +// Delete an exisiting user +// ********************************** +axiosInstance.deleteUser = async (authToken, userId) => { + return axiosInstance.delete(`/auth/user/${userId}`, { + headers: { + Authorization: `Bearer ${authToken}`, + "Content-Type": "application/json", + }, + }); +}; + +// ********************************** +// Forgot password request +// ********************************** +axiosInstance.forgotPassword = async (form) => { + return axiosInstance.post(`/auth/recovery/request`, form); +}; + +axiosInstance.validateRecoveryToken = async (recoveryToken) => { + return axiosInstance.post("/auth/recovery/validate", { + recoveryToken, + }); +}; + +// ********************************** +// Set new password request +// ********************************** +axiosInstance.setNewPassword = async (recoveryToken, form) => { + return axiosInstance.post("/auth/recovery/reset", { + ...form, + recoveryToken, + }); +}; + export default axiosInstance; diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js index 93c83c6b8..8efe2ae19 100644 --- a/Server/controllers/authController.js +++ b/Server/controllers/authController.js @@ -450,6 +450,8 @@ const deleteUserController = async (req, res, next) => { } // 1. Find all the monitors associated with the user id + console.log("wtf"); + const monitors = await req.db.getMonitorsByUserId({ params: { userId: _id }, }); @@ -459,6 +461,7 @@ const deleteUserController = async (req, res, next) => { await Promise.all( monitors.map(async (monitor) => { await req.jobQueue.deleteJob(monitor); + await req.db.deleteChecks(monitor._id); await req.db.deleteAlertByMonitorId(monitor._id); await req.db.deletePageSpeedChecksByMonitorId(monitor._id); diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js index 0fc56c755..4d84700ca 100644 --- a/Server/db/mongo/modules/monitorModule.js +++ b/Server/db/mongo/modules/monitorModule.js @@ -294,7 +294,7 @@ const getMonitorById = async (monitorId) => { */ const getMonitorsByUserId = async (req, res) => { try { - let { limit, type, status, sortOrder, normalize } = req.query; + let { limit, type, status, sortOrder, normalize } = req.query || {}; const monitorQuery = { userId: req.params.userId }; if (type !== undefined) { diff --git a/Server/db/mongo/modules/recoveryModule.js b/Server/db/mongo/modules/recoveryModule.js index c1cc23b94..b643a1664 100644 --- a/Server/db/mongo/modules/recoveryModule.js +++ b/Server/db/mongo/modules/recoveryModule.js @@ -1,3 +1,4 @@ +const UserModel = require("../../../models/user"); const RecoveryToken = require("../../../models/RecoveryToken"); const crypto = require("crypto"); const { errorMessages } = require("../../../utils/messages"); diff --git a/Server/index.js b/Server/index.js index 2cb941ed4..9a5f1797f 100644 --- a/Server/index.js +++ b/Server/index.js @@ -19,7 +19,7 @@ const JobQueue = require("./service/jobQueue"); const NetworkService = require("./service/networkService"); const EmailService = require("./service/emailService"); const PageSpeedService = require("./service/pageSpeedService"); - +const SERVICE_NAME = "Server"; let cleaningUp = false; // Need to wrap server setup in a function to handle async nature of JobQueue