diff --git a/frontend/src/API/API.ts b/frontend/src/API/API.ts index 4ebd859..d330f6d 100644 --- a/frontend/src/API/API.ts +++ b/frontend/src/API/API.ts @@ -1065,7 +1065,7 @@ export const api: API = { ): Promise> { try { const response = await fetch( - `/api/changePassword/${username}?newPassword=${newPassword}`, + `/api/changeUserPassword/${username}?newPassword=${newPassword}`, { method: "PUT", headers: { diff --git a/frontend/src/Components/ChangeUserPassword.tsx b/frontend/src/Components/ChangeUserPassword.tsx new file mode 100644 index 0000000..0bc5222 --- /dev/null +++ b/frontend/src/Components/ChangeUserPassword.tsx @@ -0,0 +1,36 @@ +import { APIResponse, api } from "../API/API"; + +/** + * Changes the password of a user + * @param {string} props.username - The username of the user + * @param {string} props.newPassword - The new password + * @returns {void} - Nothing + */ +export default function ChangeUserPassword(props: { + username: string; + newPassword: string; +}): void { + if (props.username === localStorage.getItem("username")) { + alert("You cannot change admin password"); + return; + } + api + .changeUserPassword( + props.username, + props.newPassword, + localStorage.getItem("accessToken") ?? "", + ) + .then((response: APIResponse) => { + if (response.success) { + alert("Password changed successfully"); + location.reload(); + } else { + alert("Password not changed"); + console.error(response.message); + } + }) + .catch((error) => { + alert("Password not changed"); + console.error("An error occurred during change:", error); + }); +} diff --git a/frontend/src/Components/UserInfoModal.tsx b/frontend/src/Components/UserInfoModal.tsx index 764d176..460ef85 100644 --- a/frontend/src/Components/UserInfoModal.tsx +++ b/frontend/src/Components/UserInfoModal.tsx @@ -12,6 +12,7 @@ import { usernameLowLimit, usernameUpLimit, } from "../Data/constants"; +import ChangeUserPassword from "./ChangeUserPassword"; function UserInfoModal(props: { isVisible: boolean; @@ -94,9 +95,10 @@ function UserInfoModal(props: { if ( confirm(`Are you sure you want to change password of ${props.username}?`) ) { - //TODO: insert change password functionality - alert("Not implemented yet"); - setNewPassword(""); + ChangeUserPassword({ + username: props.username, + newPassword: newPassword, + }); } else { alert("Password was not changed!"); }