From 42a5d2de33564c060d2d6f3cefdd3cf21c26a996 Mon Sep 17 00:00:00 2001 From: l3wdfut4pwr Date: Mon, 27 Apr 2026 22:43:13 +0300 Subject: add password change --- src/lib/api/ChangePassword.tsx | 54 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/lib/api/ChangePassword.tsx (limited to 'src/lib/api/ChangePassword.tsx') diff --git a/src/lib/api/ChangePassword.tsx b/src/lib/api/ChangePassword.tsx new file mode 100644 index 0000000..93900b9 --- /dev/null +++ b/src/lib/api/ChangePassword.tsx @@ -0,0 +1,54 @@ +const API_URL = process.env.NEXT_PUBLIC_API_URL; + +export type ChangePasswordResponse = + | { data: { success: true }; error: null } + | { data: null; error: { general: string } }; + +export const changePassword = async ( + currentPassword: string, + newPassword: string, + repeatPassword: string, +): Promise => { + try { + const res = await fetch(`${API_URL}/api/users/password`, { + method: 'PATCH', + headers: { + 'Content-Type': 'application/json', + }, + credentials: 'include', + body: JSON.stringify({ + current_password: currentPassword, + new_password: newPassword, + repeat_password: repeatPassword, + }), + }); + + const data = await res.json().catch(() => null); + + if (!res.ok) { + const detail = data?.detail; + + return { + data: null, + error: { + general: + typeof detail === 'string' + ? detail + : detail?.msg || 'Ошибка смены пароля', + }, + }; + } + + return { + data: { success: true }, + error: null, + }; + } catch (err: any) { + return { + data: null, + error: { + general: err?.message || 'Network error', + }, + }; + } +}; -- cgit v1.3-3-g829e