"use client" import type React from "react" import { useState, useEffect } from "react" import Cookies from "js-cookie" import { useRouter } from "next/navigation" import axios from "axios" import { AlertCircle, KeyRound, Mail, User } from "lucide-react" import Link from "next/link" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert" import {useTranslations} from 'next-intl'; export default function Home() { const t = useTranslations('Home'); const [username, setUsername] = useState("") const [password, setPassword] = useState("") const [rememberMe, setRememberMe] = useState(false) const router = useRouter() const [error, setError] = useState("") const [errorVisible, setErrorVisible] = useState(false) const [isLoading, setIsLoading] = useState(false) useEffect(() => { const token = Cookies.get("token") if (token) { router.push("/dashboard") } }, [router]) interface LoginResponse { token: string } const login = async () => { if (!username || !password) { setError("Please enter both email and password") setErrorVisible(true) return } try { setIsLoading(true) const response = await axios.post("/api/auth/login", { username, password }) const { token } = response.data as LoginResponse const cookieOptions = rememberMe ? { expires: 7 } : {} Cookies.set("token", token, cookieOptions) router.push("/dashboard") } catch (error: any) { setError(error.response?.data?.error || "Login failed. Please try again.") setErrorVisible(true) } finally { setIsLoading(false) } } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter") { login() } } return (

CoreControl

{t('LoginCardDescription')}

{t('LoginCardTitle')} {t('LoginCardDescription')} {errorVisible && ( {t('AuthenticationError')} {error} )}
setUsername(e.target.value)} onKeyDown={handleKeyDown} required />
setPassword(e.target.value)} onKeyDown={handleKeyDown} required />
) }