From dfe459ce1d5e9ce6a2b28c4ae94daf38e7f278a1 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira Date: Fri, 8 Sep 2023 16:47:16 -0300 Subject: [PATCH] Fixed changing password not working --- src/frontend/src/controllers/API/index.ts | 15 +++++++++++++ .../src/pages/ProfileSettingsPage/index.tsx | 22 +++++++++---------- src/frontend/src/types/api/index.ts | 5 +++++ 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/frontend/src/controllers/API/index.ts b/src/frontend/src/controllers/API/index.ts index 2806d3e25..b7aaf8a00 100644 --- a/src/frontend/src/controllers/API/index.ts +++ b/src/frontend/src/controllers/API/index.ts @@ -7,6 +7,7 @@ import { LoginType, Users, changeUser, + resetPasswordType, sendAllProps, } from "../../types/api/index"; import { UserInputType } from "../../types/components"; @@ -472,6 +473,20 @@ export async function updateUser(user_id: string, user: changeUser) { } } +export async function resetPassword(user_id: string, user: resetPasswordType) { + try { + const res = await api.patch(`${BASE_URL_API}users/${user_id}/reset-password`, user); + if (res.status === 200) { + return res.data; + } else { + throw new Error(res.data.toString()); + } + } catch (error) { + console.log("Error:", error); + throw error; + } +} + export async function getApiKey() { try { const res = await api.get(`${BASE_URL_API}api_key/`); diff --git a/src/frontend/src/pages/ProfileSettingsPage/index.tsx b/src/frontend/src/pages/ProfileSettingsPage/index.tsx index 5bf63b807..3f89dfa7f 100644 --- a/src/frontend/src/pages/ProfileSettingsPage/index.tsx +++ b/src/frontend/src/pages/ProfileSettingsPage/index.tsx @@ -10,7 +10,7 @@ import { CONTROL_PATCH_USER_STATE } from "../../constants/constants"; import { alertContext } from "../../contexts/alertContext"; import { AuthContext } from "../../contexts/authContext"; import { TabsContext } from "../../contexts/tabsContext"; -import { updateUser } from "../../controllers/API"; +import { resetPassword, updateUser } from "../../controllers/API"; import { inputHandlerEventType, patchUserInputStateType, @@ -31,7 +31,7 @@ export default function ProfileSettingsPage(): JSX.Element { const { userData, setUserData } = useContext(AuthContext); const { password, cnfPassword, gradient } = inputState; - function handlePatchUser() { + async function handlePatchUser() { if (password !== cnfPassword) { setErrorData({ title: "Error changing password", @@ -40,17 +40,15 @@ export default function ProfileSettingsPage(): JSX.Element { return; } try { - updateUser(userData!.id, { password, profile_image: gradient }).then( - () => { - setSuccessData({ title: "Changes saved successfully!" }); - if (gradient !== "") { - let newUserData = cloneDeep(userData); - newUserData!.profile_image = gradient; + await resetPassword(userData!.id, { password }); + await updateUser(userData!.id, { profile_image: gradient }); + if (gradient !== "") { + let newUserData = cloneDeep(userData); + newUserData!.profile_image = gradient; - setUserData(newUserData); - } - } - ); + setUserData(newUserData); + } + setSuccessData({ title: "Changes saved successfully!" }); } catch (error) { setErrorData({ title: "Error saving changes", list: [error as string] }); } diff --git a/src/frontend/src/types/api/index.ts b/src/frontend/src/types/api/index.ts index d693badf0..244ecbefe 100644 --- a/src/frontend/src/types/api/index.ts +++ b/src/frontend/src/types/api/index.ts @@ -86,6 +86,11 @@ export type changeUser = { profile_image?: string; } +export type resetPasswordType = { + password?: string; + profile_image?: string; +} + export type Users = { id: string; username: string;