import { useState } from "react"; import { Link } from "react-router-dom"; import { ChevronLeftIcon, EyeCloseIcon, EyeIcon } from "../../icons"; import Label from "../form/Label"; import Input from "../form/input/InputField"; import Checkbox from "../form/input/Checkbox"; import Button from "../ui/button/Button"; import { useToastOnError } from "../../hooks/useToastOnError"; export default function SignInForm() { const [showPassword, setShowPassword] = useState(false); const [isChecked, setIsChecked] = useState(false); const [nip, setNip] = useState(""); const [password, setPassword] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); useToastOnError(error); function getCsrfToken() { const el = document.querySelector('meta[name="csrf-token"]'); return (el && "content" in el) ? (el as HTMLMetaElement).content : ""; } async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setLoading(true); setError(""); try { const res = await fetch("/login", { method: "POST", headers: { "Content-Type": "application/json", "X-CSRF-TOKEN": getCsrfToken(), "Accept": "application/json", }, body: JSON.stringify({ nip, password, remember: isChecked, }), }); if (res.status === 422) { const data = await res.json().catch(() => ({})); const msg = data?.message || (data?.errors ? Object.values(data.errors).flat().join(" ") : "Validasi gagal"); throw new Error(msg); } if (!res.ok) { throw new Error("NIP atau password salah."); } window.location.href = "/admin"; } catch (err: any) { const message = err?.message || "Gagal login"; setError(message); } finally { setLoading(false); } } return (
Masukkan NIP dan password untuk masuk.