Fixed changing password not working

This commit is contained in:
Lucas Oliveira 2023-09-08 16:47:16 -03:00
commit dfe459ce1d
3 changed files with 30 additions and 12 deletions

View file

@ -7,6 +7,7 @@ import {
LoginType, LoginType,
Users, Users,
changeUser, changeUser,
resetPasswordType,
sendAllProps, sendAllProps,
} from "../../types/api/index"; } from "../../types/api/index";
import { UserInputType } from "../../types/components"; 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() { export async function getApiKey() {
try { try {
const res = await api.get(`${BASE_URL_API}api_key/`); const res = await api.get(`${BASE_URL_API}api_key/`);

View file

@ -10,7 +10,7 @@ import { CONTROL_PATCH_USER_STATE } from "../../constants/constants";
import { alertContext } from "../../contexts/alertContext"; import { alertContext } from "../../contexts/alertContext";
import { AuthContext } from "../../contexts/authContext"; import { AuthContext } from "../../contexts/authContext";
import { TabsContext } from "../../contexts/tabsContext"; import { TabsContext } from "../../contexts/tabsContext";
import { updateUser } from "../../controllers/API"; import { resetPassword, updateUser } from "../../controllers/API";
import { import {
inputHandlerEventType, inputHandlerEventType,
patchUserInputStateType, patchUserInputStateType,
@ -31,7 +31,7 @@ export default function ProfileSettingsPage(): JSX.Element {
const { userData, setUserData } = useContext(AuthContext); const { userData, setUserData } = useContext(AuthContext);
const { password, cnfPassword, gradient } = inputState; const { password, cnfPassword, gradient } = inputState;
function handlePatchUser() { async function handlePatchUser() {
if (password !== cnfPassword) { if (password !== cnfPassword) {
setErrorData({ setErrorData({
title: "Error changing password", title: "Error changing password",
@ -40,17 +40,15 @@ export default function ProfileSettingsPage(): JSX.Element {
return; return;
} }
try { try {
updateUser(userData!.id, { password, profile_image: gradient }).then( await resetPassword(userData!.id, { password });
() => { await updateUser(userData!.id, { profile_image: gradient });
setSuccessData({ title: "Changes saved successfully!" });
if (gradient !== "") { if (gradient !== "") {
let newUserData = cloneDeep(userData); let newUserData = cloneDeep(userData);
newUserData!.profile_image = gradient; newUserData!.profile_image = gradient;
setUserData(newUserData); setUserData(newUserData);
} }
} setSuccessData({ title: "Changes saved successfully!" });
);
} catch (error) { } catch (error) {
setErrorData({ title: "Error saving changes", list: [error as string] }); setErrorData({ title: "Error saving changes", list: [error as string] });
} }

View file

@ -86,6 +86,11 @@ export type changeUser = {
profile_image?: string; profile_image?: string;
} }
export type resetPasswordType = {
password?: string;
profile_image?: string;
}
export type Users = { export type Users = {
id: string; id: string;
username: string; username: string;