diff options
| author | l3wdfut4pwr <l3wdfut4pwr@gmail.com> | 2026-04-27 22:43:13 +0300 |
|---|---|---|
| committer | l3wdfut4pwr <l3wdfut4pwr@gmail.com> | 2026-04-27 22:43:13 +0300 |
| commit | 42a5d2de33564c060d2d6f3cefdd3cf21c26a996 (patch) | |
| tree | 99d9b118d62c2b4d518a5ae468cb9eda29202cf1 /src/lib/api/ChangeEmail.tsx | |
| parent | 50cbca3c307894c9fd55baec522b7b794d9ab805 (diff) | |
add password change
Diffstat (limited to 'src/lib/api/ChangeEmail.tsx')
| -rw-r--r-- | src/lib/api/ChangeEmail.tsx | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/lib/api/ChangeEmail.tsx b/src/lib/api/ChangeEmail.tsx new file mode 100644 index 0000000..13d462c --- /dev/null +++ b/src/lib/api/ChangeEmail.tsx @@ -0,0 +1,49 @@ +const API_URL = process.env.NEXT_PUBLIC_API_URL; + +type ChangeEmailResponse = + | { data: { email: string }; error: null } + | { data: null; error: { general: string } }; + +export const changeEmail = async ( + email: string, + password: string, +): Promise<ChangeEmailResponse> => { + try { + const res = await fetch(`${API_URL}/api/users/email`, { + method: 'PATCH', + headers: { + 'Content-Type': 'application/json', + }, + credentials: 'include', + body: JSON.stringify({ email, password }), + }); + + 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, + error: null, + }; + } catch (err: any) { + return { + data: null, + error: { + general: err?.message || 'Network error', + }, + }; + } +}; |
