"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || "http://127.0.0.1:18000"; export default function LoginPage() { const [login, setLogin] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(""); const [showPassword, setShowPassword] = useState(false); const [isSubmitting, setIsSubmitting] = useState(false); const router = useRouter(); const handleSubmit = async (e) => { e.preventDefault(); setError(""); setIsSubmitting(true); try { const res = await fetch(`${API_BASE_URL}/api/auth/login`, { method: "POST", headers: { "Content-Type": "application/json" }, credentials: "include", body: JSON.stringify({ login, password }), }); if (!res.ok) { let message = "Неверный логин или пароль"; try { const data = await res.json(); if (data?.detail && typeof data.detail === "string") { message = data.detail; } } catch { // ignore malformed/non-json body } throw new Error(message); } router.push("/"); router.refresh(); } catch (err) { setError(err?.message || "Ошибка входа"); } finally { setIsSubmitting(false); } }; return (