From 468a67f937353580e10c5ef370f403de6bf70285 Mon Sep 17 00:00:00 2001 From: karenvicent Date: Wed, 1 Oct 2025 11:34:51 -0400 Subject: [PATCH] git stash push -m "work in progress" --- client/src/Hooks/v1/userHooks.js | 26 +++- .../components/ChangePasswordModal/index.jsx | 140 ++++++++++++++++++ .../src/Pages/v1/Account/EditUser/index.jsx | 19 ++- client/src/Utils/NetworkService.js | 10 ++ client/src/locales/en.json | 8 +- server/src/controllers/v1/authController.js | 23 +++ server/src/routes/v1/authRoute.js | 1 + server/src/service/v1/business/userService.js | 5 + server/src/validation/joi.js | 5 + 9 files changed, 230 insertions(+), 7 deletions(-) create mode 100644 client/src/Pages/Account/components/ChangePasswordModal/index.jsx diff --git a/client/src/Hooks/v1/userHooks.js b/client/src/Hooks/v1/userHooks.js index 71b156594..f9c454450 100644 --- a/client/src/Hooks/v1/userHooks.js +++ b/client/src/Hooks/v1/userHooks.js @@ -7,7 +7,6 @@ export const useGetUser = (userId) => { const [user, setUser] = useState(undefined); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); - const fetchUser = useCallback(async () => { try { setIsLoading(true); @@ -54,8 +53,29 @@ export const useEditUser = (userId) => { setIsLoading(false); } }, - [userId] + [userId, t] ); - return [editUser, isLoading, error]; + const changePassword = useCallback( + async (passwordForm) => { + try { + setIsLoading(true); + + await networkService.changePasswordByAdmin({ userId, passwordForm }); + createToast({ + body: t("teamPanel.changeTeamPassword.success"), + }); + } catch (error) { + createToast({ + body: error.message, + }); + setError(error); + } finally { + setIsLoading(false); + } + }, + [userId, t] + ); + + return [editUser, isLoading, error, changePassword]; }; diff --git a/client/src/Pages/Account/components/ChangePasswordModal/index.jsx b/client/src/Pages/Account/components/ChangePasswordModal/index.jsx new file mode 100644 index 000000000..375595b29 --- /dev/null +++ b/client/src/Pages/Account/components/ChangePasswordModal/index.jsx @@ -0,0 +1,140 @@ +import { useState } from "react"; +import { Button, Stack } from "@mui/material"; +import { GenericDialog } from "../../../../Components/Dialog/genericDialog"; +import TextInput from "../../../../Components/Inputs/TextInput"; +import PasswordTooltip from "../../../Auth/components/PasswordTooltip"; +import { useTheme } from "@emotion/react"; +import { useTranslation } from "react-i18next"; +import { createToast } from "../../../../Utils/toastUtils"; +import { PasswordEndAdornment } from "../../../../Components/Inputs/TextInput/Adornments"; +import usePasswordFeedback from "../../../Auth/hooks/usePasswordFeedback"; + +const ChangePasswordModal = ({ isSaving, isLoading, userId, changePassword, email }) => { + const INITIAL_FORM_STATE = { + password: "", + confirm: "", + }; + console.log("email", email); + const theme = useTheme(); + const { t } = useTranslation(); + const { feedback, handlePasswordFeedback } = usePasswordFeedback(); + const [form, setForm] = useState(INITIAL_FORM_STATE); + const [isChangePasswordOpen, setIsChangePasswordOpen] = useState(false); + const [errors, setErrors] = useState({}); + const [isLoadingSubmit, setIsLoadingSubmit] = useState(false); + const closeChangePasswordModal = () => { + setIsChangePasswordOpen(false); + setForm(INITIAL_FORM_STATE); + }; + + const onChange = (e) => { + let { name, value } = e.target; + const updatedForm = { ...form, [name]: value }; + //validateFields(name, value, updatedForm); + setForm(updatedForm); + + handlePasswordFeedback(updatedForm, name, value, form, errors, setErrors); + }; + const isFormValid = !errors.password && !errors.confirm; + const onsubmitChangePassword = async (event) => { + event.preventDefault(); + if (!isFormValid) return; + const newPasswordForm = { + password: form.password, + newPassword: form.confirm, + email: email, + }; + try { + setIsLoadingSubmit(true); + await changePassword(newPasswordForm); + closeChangePasswordModal(); + } catch (error) { + const errorMsg = error.response?.data?.msg || error.message || "unknownError"; + createToast({ + type: "error", + body: t(errorMsg), + }); + } finally { + setIsLoadingSubmit(false); + } + }; + return ( + <> + + + + } + sx={{ mb: theme.spacing(5) }} + /> + + } + sx={{ mb: theme.spacing(5) }} + /> + + + + + + + + ); +}; +export default ChangePasswordModal; diff --git a/client/src/Pages/v1/Account/EditUser/index.jsx b/client/src/Pages/v1/Account/EditUser/index.jsx index e2e976916..9fe307cf2 100644 --- a/client/src/Pages/v1/Account/EditUser/index.jsx +++ b/client/src/Pages/v1/Account/EditUser/index.jsx @@ -7,6 +7,7 @@ import TextInput from "@/Components/v1/Inputs/TextInput/index.jsx"; import Search from "@/Components/v1/Inputs/Search/index.jsx"; import Button from "@mui/material/Button"; import RoleTable from "../components/RoleTable/index.jsx"; +import ChangePasswordModal from "../components/ChangePasswordModal"; // Utils import { useParams } from "react-router-dom"; @@ -26,7 +27,7 @@ const EditUser = () => { ]; const [user, isLoading, error] = useGetUser(userId); - const [editUser, isSaving, saveError] = useEditUser(userId); + const [editUser, isSaving, saveError, changePassword] = useEditUser(userId); const [ form, setForm, @@ -104,7 +105,12 @@ const EditUser = () => { roles={form?.role} handleDeleteRole={handleDeleteRole} /> - + - + + ); diff --git a/client/src/Utils/NetworkService.js b/client/src/Utils/NetworkService.js index dce0faaf5..7401994bb 100644 --- a/client/src/Utils/NetworkService.js +++ b/client/src/Utils/NetworkService.js @@ -1136,6 +1136,16 @@ class NetworkService { }, }); } + + async changePasswordByAdmin(config) { + const { userId, passwordForm } = config; + console.log("Password form data:", passwordForm); + return this.axiosInstance.put(`auth/users/${userId}/password`, passwordForm, { + headers: { + "Content-Type": "application/json", + }, + }); + } } export default NetworkService; diff --git a/client/src/locales/en.json b/client/src/locales/en.json index d89ebc44e..642c8e0f6 100644 --- a/client/src/locales/en.json +++ b/client/src/locales/en.json @@ -425,7 +425,13 @@ } } }, - "role": "Role" + "role": "Role", + "changeTeamPassword": { + "changePasswordMenu": "Reset Password", + "title": "Reset team member password", + "description": "Create a new password for this team member. You will need to share the password with them securely.", + "success": "Password successfully reset. Make sure to provide the credentials to the member in a secure way." + } }, "monitorState": { "paused": "Paused", diff --git a/server/src/controllers/v1/authController.js b/server/src/controllers/v1/authController.js index 17ed517eb..b84aaacd6 100755 --- a/server/src/controllers/v1/authController.js +++ b/server/src/controllers/v1/authController.js @@ -456,6 +456,29 @@ class AuthController extends BaseController { SERVICE_NAME, "editUserById" ); + editUserPasswordById = this.asyncHandler( + async (req, res) => { + console.log("editUserPasswordById called"); + const roles = req?.user?.role; + if (!roles.includes("superadmin")) { + console.log("entro a error editUserPasswordById"); + throw createError("Unauthorized", 403); + } + + const userId = req.params.userId; + console.log("userId param:", userId); + const updates = { ...req.body }; + console.log("newPassword body:", req.body); + await editUserByIdParamValidation.validateAsync(req.params); + console.log("params validated"); + //await editUserPasswordByIdBodyValidation.validateAsync(req.body); + console.log("body validated"); + await this.userService.setPasswordByUserId( userId, updates ); + return res.success({ msg: "Password reset successfully" }); + }, + SERVICE_NAME, "editUserPasswordById" + ) + } export default AuthController; diff --git a/server/src/routes/v1/authRoute.js b/server/src/routes/v1/authRoute.js index d9b036b05..63b4c4201 100755 --- a/server/src/routes/v1/authRoute.js +++ b/server/src/routes/v1/authRoute.js @@ -25,6 +25,7 @@ class AuthRoutes { this.router.get("/users", verifyJWT, isAllowed(["admin", "superadmin"]), this.authController.getAllUsers); this.router.get("/users/:userId", verifyJWT, isAllowed(["superadmin"]), this.authController.getUserById); this.router.put("/users/:userId", verifyJWT, isAllowed(["superadmin"]), this.authController.editUserById); + this.router.put("/users/:userId/password", verifyJWT, isAllowed(["superadmin"]), this.authController.editUserPasswordById); this.router.put("/user", verifyJWT, upload.single("profileImage"), this.authController.editUser); this.router.delete("/user", verifyJWT, this.authController.deleteUser); diff --git a/server/src/service/v1/business/userService.js b/server/src/service/v1/business/userService.js index 0a965bced..6c036a5c8 100644 --- a/server/src/service/v1/business/userService.js +++ b/server/src/service/v1/business/userService.js @@ -211,5 +211,10 @@ class UserService { editUserById = async (userId, user) => { await this.db.userModule.editUserById(userId, user); }; + setPasswordByUserId = async (userId, updates) => { + const updatedUser = await this.db.userModule.updateUser({ userId: userId, user: updates, file: null }); + return updatedUser; + } + } export default UserService; diff --git a/server/src/validation/joi.js b/server/src/validation/joi.js index fa8dccde2..209135769 100755 --- a/server/src/validation/joi.js +++ b/server/src/validation/joi.js @@ -670,6 +670,10 @@ const editSuperadminUserByIdBodyValidation = joi.object({ .required(), }); +const editUserPasswordByIdBodyValidation = joi.object({ + newPassword: joi.string().min(8).required().pattern(passwordPattern), +}); + export { roleValidatior, loginValidation, @@ -739,4 +743,5 @@ export { editUserByIdParamValidation, editUserByIdBodyValidation, editSuperadminUserByIdBodyValidation, + editUserPasswordByIdBodyValidation, };