summaryrefslogtreecommitdiff
path: root/src/components/header/authdialog/LoginForm.tsx
diff options
context:
space:
mode:
authorl3wdfut4pwr <l3wdfut4pwr@gmail.com>2026-04-02 08:36:51 +0300
committerl3wdfut4pwr <l3wdfut4pwr@gmail.com>2026-04-02 08:36:51 +0300
commit1d20080db8a26e4b7dd4071daac1166142592afa (patch)
treeb4c1bbb3725f00515b4d9a3d1cd18584cba36c81 /src/components/header/authdialog/LoginForm.tsx
parent37f51ee88710868a77b4645294cf32862f55e7c4 (diff)
minor improvements
Diffstat (limited to 'src/components/header/authdialog/LoginForm.tsx')
-rw-r--r--src/components/header/authdialog/LoginForm.tsx22
1 files changed, 9 insertions, 13 deletions
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<any>({});
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 && (
- <p className="text-red pl-5 text-[12px] leading-[16px] mt-[5px]">
- {errors.password}
- </p>
- )}
{!errors.password && errors.general && (
<p className="text-red pl-5 text-[12px] leading-[16px] mt-[5px]">
{errors.general}