From 1d20080db8a26e4b7dd4071daac1166142592afa Mon Sep 17 00:00:00 2001 From: l3wdfut4pwr Date: Thu, 2 Apr 2026 08:36:51 +0300 Subject: minor improvements --- src/components/header/authdialog/LoginForm.tsx | 22 ++++++--------- src/components/header/authdialog/register.ts | 39 ++++++++------------------ 2 files changed, 20 insertions(+), 41 deletions(-) (limited to 'src/components/header/authdialog') diff --git a/src/components/header/authdialog/LoginForm.tsx b/src/components/header/authdialog/LoginForm.tsx index ceadbcd..c6cdbcd 100644 --- a/src/components/header/authdialog/LoginForm.tsx +++ b/src/components/header/authdialog/LoginForm.tsx @@ -6,6 +6,7 @@ import { InputField } from '@/components/ui/inputfield'; import { useAuthContext } from '@/lib/contexts/Auth.context'; export default function LoginForm() { + const API_URL = process.env.NEXT_PUBLIC_API_URL; const [errors, setErrors] = useState({}); const [loading, setLoading] = useState(false); const { setUser } = useAuthContext(); @@ -36,27 +37,27 @@ export default function LoginForm() { body.append('password', password); try { - const res = await fetch('http://localhost:8000/api/auth/login', { + const res = await fetch(`${API_URL}/api/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, body: body.toString(), + credentials: 'include', }); - const data = await res.json(); - if (!res.ok) { setErrors({ general: 'Неверный логин или пароль' }); - } else if (data.access_token) { - localStorage.setItem('token', data.access_token); - - const meRes = await fetch('http://localhost:8000/api/me', { - headers: { Authorization: `Bearer ${data.access_token}` }, + } else { + const meRes = await fetch(`${API_URL}/api/me`, { + credentials: 'include', }); + if (!meRes.ok) throw new Error('Failed to fetch user'); const meData = await meRes.json(); setUser(meData); } + } catch (err) { + setErrors({ general: 'Ошибка при авторизации' }); } finally { setLoading(false); } @@ -91,11 +92,6 @@ export default function LoginForm() { placeholder="Пароль" name="password" /> - {errors.password && ( -

- {errors.password} -

- )} {!errors.password && errors.general && (

{errors.general} diff --git a/src/components/header/authdialog/register.ts b/src/components/header/authdialog/register.ts index ba8bceb..46a59f2 100644 --- a/src/components/header/authdialog/register.ts +++ b/src/components/header/authdialog/register.ts @@ -1,4 +1,3 @@ -// register.ts export const validate = ( username: string, email: string, @@ -36,20 +35,23 @@ export const validate = ( return newErrors; }; +const API_URL = process.env.NEXT_PUBLIC_API_URL; + export const registerUser = async ( username: string, email: string, password: string, ) => { try { - const res = await fetch('http://localhost:8000/api/auth/register', { + const res = await fetch(`${API_URL}/api/auth/register`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, email, password }), + credentials: 'include', }); - const data = await res.json(); if (!res.ok) { + const data = await res.json(); const { field, message } = data.detail || {}; return { data: null, @@ -59,35 +61,16 @@ export const registerUser = async ( }; } - const body = new URLSearchParams(); - body.append('username', username); - body.append('password', password); - - const loginRes = await fetch('http://localhost:8000/api/auth/login', { - method: 'POST', - headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, - body: body.toString(), + const meRes = await fetch(`${API_URL}/api/me`, { + credentials: 'include', }); - const loginData = await loginRes.json(); - - if (!loginRes.ok) { - return { - data: null, - error: { - general: - loginData.detail || 'Ошибка входа после регистрации', - }, - }; + if (!meRes.ok) { + throw new Error( + 'Не удалось получить данные пользователя после регистрации', + ); } - localStorage.setItem('token', loginData.access_token); - localStorage.setItem('refresh_token', loginData.refresh_token); - - const meRes = await fetch('http://localhost:8000/api/me', { - headers: { Authorization: `Bearer ${loginData.access_token}` }, - }); - const meData = await meRes.json(); return { data: meData, error: null }; -- cgit v1.3-3-g829e