From e619245f1fa83a29a9ec553ef9017871bb5c27c0 Mon Sep 17 00:00:00 2001 From: l3wdfut4pwr Date: Wed, 29 Apr 2026 02:07:46 +0300 Subject: add google auth --- src/components/header/authdialog/LoginForm.tsx | 12 +++- src/components/header/authdialog/RegisterForm.tsx | 12 +++- src/components/settings/ChangePassword.tsx | 87 ++++++++++++----------- src/components/settings/SecurityPage.tsx | 26 +------ src/components/settings/SetPasswordField.tsx | 76 ++++++++++++++++++++ src/lib/api/RefreshUser.ts | 37 ++++++++++ src/lib/api/SetPassword.ts | 52 ++++++++++++++ 7 files changed, 235 insertions(+), 67 deletions(-) create mode 100644 src/components/settings/SetPasswordField.tsx create mode 100644 src/lib/api/RefreshUser.ts create mode 100644 src/lib/api/SetPassword.ts diff --git a/src/components/header/authdialog/LoginForm.tsx b/src/components/header/authdialog/LoginForm.tsx index b4d90f1..3a42979 100644 --- a/src/components/header/authdialog/LoginForm.tsx +++ b/src/components/header/authdialog/LoginForm.tsx @@ -6,6 +6,10 @@ import { InputField } from '@/components/ui/inputfield'; import { useAuthContext } from '@/lib/contexts/Auth.context'; import { useRouter } from 'next/navigation'; export default function LoginForm({ redirectTo }: { redirectTo?: string }) { + const handleGoogleLogin = () => { + window.location.href = `${API_URL}/api/auth/google/login`; + }; + const API_URL = process.env.NEXT_PUBLIC_API_URL; const [errors, setErrors] = useState({}); const [loading, setLoading] = useState(false); @@ -70,9 +74,13 @@ export default function LoginForm({ redirectTo }: { redirectTo?: string }) { onSubmit={handleSubmit} className="flex flex-col gap-5 min-w-[310px] w-fit" > -
diff --git a/src/components/header/authdialog/RegisterForm.tsx b/src/components/header/authdialog/RegisterForm.tsx index ef22433..604b26c 100644 --- a/src/components/header/authdialog/RegisterForm.tsx +++ b/src/components/header/authdialog/RegisterForm.tsx @@ -8,6 +8,12 @@ import { useAuthContext } from '@/lib/contexts/Auth.context'; import Image from 'next/image'; import { useRouter } from 'next/navigation'; export default function RegisterForm({ redirectTo }: { redirectTo?: string }) { + const API_URL = process.env.NEXT_PUBLIC_API_URL; + + const handleGoogleLogin = () => { + window.location.href = `${API_URL}/api/auth/google/login`; + }; + const [errors, setErrors] = useState({}); const [loading, setLoading] = useState(false); const router = useRouter(); @@ -53,7 +59,11 @@ export default function RegisterForm({ redirectTo }: { redirectTo?: string }) { onSubmit={handleSubmit} className="flex flex-col gap-5 min-w-[310px] w-fit" > - diff --git a/src/components/settings/ChangePassword.tsx b/src/components/settings/ChangePassword.tsx index d437b5e..7721c85 100644 --- a/src/components/settings/ChangePassword.tsx +++ b/src/components/settings/ChangePassword.tsx @@ -21,8 +21,8 @@ export const ChangePasswordField = () => { return; } - if (newPassword.length < 6) { - setError('Пароль должен быть минимум 6 символов'); + if (newPassword.length < 8) { + setError('Пароль должен быть минимум 8 символов'); return; } @@ -47,45 +47,50 @@ export const ChangePasswordField = () => { }; return ( -
-

СМЕНА ПАРОЛЯ

- -
- setCurrentPassword(e.target.value)} - /> - - setNewPassword(e.target.value)} - /> - - setConfirmPassword(e.target.value)} - /> + <> +
+

СМЕНА ПАРОЛЯ

+ +
+ + setCurrentPassword(e.target.value) + } + /> + + setNewPassword(e.target.value)} + /> + + + setConfirmPassword(e.target.value) + } + /> +
+ + {error &&

{error}

} + +
- - {error &&

{error}

} - - - - -
+ + ); }; diff --git a/src/components/settings/SecurityPage.tsx b/src/components/settings/SecurityPage.tsx index dd650c4..9c498aa 100644 --- a/src/components/settings/SecurityPage.tsx +++ b/src/components/settings/SecurityPage.tsx @@ -6,6 +6,7 @@ import LogoutButton from './LogoutButton'; import { changeEmail } from '@/lib/api/ChangeEmail'; import { useState } from 'react'; import { ChangePasswordField } from './ChangePassword'; +import { SetPasswordField } from './SetPasswordField'; export default function SecurityPage() { const { user } = useAuthContext(); @@ -42,28 +43,7 @@ export default function SecurityPage() { <>
- {showSetPassword && ( -
-

- ЗАДАТЬ ПАРОЛЬ -

-
- - -
- - -
- )} + {showSetPassword && }

@@ -91,7 +71,7 @@ export default function SecurityPage() {

- + {hasPassword && }
diff --git a/src/components/settings/SetPasswordField.tsx b/src/components/settings/SetPasswordField.tsx new file mode 100644 index 0000000..6030c8f --- /dev/null +++ b/src/components/settings/SetPasswordField.tsx @@ -0,0 +1,76 @@ +'use client'; + +import { useState } from 'react'; +import { InputField } from '@/components/ui/inputfield'; +import { Button } from '@/components/ui/button'; +import { Separator } from '@/components/ui/separator'; +import { setPassword } from '@/lib/api/SetPassword'; + +export const SetPasswordField = () => { + const [newPassword, setNewPassword] = useState(''); + const [repeatPassword, setRepeatPassword] = useState(''); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); + + const handleSubmit = async () => { + setError(null); + + if (newPassword !== repeatPassword) { + setError('Пароли не совпадают'); + return; + } + + if (newPassword.length < 8) { + setError('Пароль должен быть минимум 8 символов'); + return; + } + + setLoading(true); + + const res = await setPassword(newPassword, repeatPassword); + + setLoading(false); + + if (res.error) { + setError(res.error.general); + return; + } + + setNewPassword(''); + setRepeatPassword(''); + }; + + return ( +
+

ЗАДАТЬ ПАРОЛЬ

+ +
+ setNewPassword(e.target.value)} + /> + + setRepeatPassword(e.target.value)} + /> +
+ + {error &&

{error}

} + + + + +
+ ); +}; diff --git a/src/lib/api/RefreshUser.ts b/src/lib/api/RefreshUser.ts new file mode 100644 index 0000000..d93d310 --- /dev/null +++ b/src/lib/api/RefreshUser.ts @@ -0,0 +1,37 @@ +const API_URL = process.env.NEXT_PUBLIC_API_URL; + +export type RefreshUserResponse = { + authenticated: boolean; + user: any | null; +}; + +export const refreshUser = async (): Promise => { + try { + const res = await fetch(`${API_URL}/api/me`, { + method: 'GET', + credentials: 'include', + headers: { + 'Content-Type': 'application/json', + }, + }); + + const data = await res.json().catch(() => null); + + if (!res.ok || !data) { + return { + authenticated: false, + user: null, + }; + } + + return { + authenticated: data.authenticated, + user: data.authenticated ? data.user : null, + }; + } catch (err) { + return { + authenticated: false, + user: null, + }; + } +}; diff --git a/src/lib/api/SetPassword.ts b/src/lib/api/SetPassword.ts new file mode 100644 index 0000000..ee445fc --- /dev/null +++ b/src/lib/api/SetPassword.ts @@ -0,0 +1,52 @@ +const API_URL = process.env.NEXT_PUBLIC_API_URL; + +export type SetPasswordResponse = + | { data: { success: true }; error: null } + | { data: null; error: { general: string } }; + +export const setPassword = async ( + newPassword: string, + repeatPassword: string, +): Promise => { + try { + const res = await fetch(`${API_URL}/api/users/password/set`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + credentials: 'include', + body: JSON.stringify({ + 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