'use client' import { useState, useRef } from 'react' import { useRouter } from 'next/navigation' interface Props { from: string } export default function LoginForm({ from }: Props) { const router = useRouter() const [error, setError] = useState('') const [loading, setLoading] = useState(false) const usernameRef = useRef(null) const passwordRef = useRef(null) async function handleSubmit(e: React.FormEvent) { e.preventDefault() setError('') setLoading(true) const username = usernameRef.current?.value.trim() ?? '' const password = passwordRef.current?.value ?? '' try { const res = await fetch('/api/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password }), }) const data = (await res.json()) as { error?: string } if (!res.ok) { setError(data.error ?? 'Login failed') setLoading(false) return } // Navigate to intended destination or browse router.push(from || '/browse') router.refresh() } catch { setError('Network error — please try again') setLoading(false) } } return (
{error && (
{error}
)}
(e.currentTarget.style.borderColor = 'rgba(34,211,238,0.5)') } onBlur={(e) => (e.currentTarget.style.borderColor = 'rgba(255,255,255,0.1)') } placeholder="Enter your username" />
(e.currentTarget.style.borderColor = 'rgba(34,211,238,0.5)') } onBlur={(e) => (e.currentTarget.style.borderColor = 'rgba(255,255,255,0.1)') } placeholder="Enter your password" />
) }