diff options
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/api/ChangeEmail.tsx | 49 | ||||
| -rw-r--r-- | src/lib/api/ChangePassword.tsx | 54 | ||||
| -rw-r--r-- | src/lib/contexts/Auth.context.tsx | 2 |
3 files changed, 104 insertions, 1 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', + }, + }; + } +}; 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<ChangePasswordResponse> => { + 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', + }, + }; + } +}; diff --git a/src/lib/contexts/Auth.context.tsx b/src/lib/contexts/Auth.context.tsx index 127c942..e5ed643 100644 --- a/src/lib/contexts/Auth.context.tsx +++ b/src/lib/contexts/Auth.context.tsx @@ -5,7 +5,7 @@ export type User = { id: number; username: string; email: string; - password: boolean; + has_password: boolean; google_id?: string | null; avatar?: string; banner_file?: string; |
