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,
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/`);

View file

@ -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] });
}

View file

@ -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;