diff --git a/app/page.tsx b/app/page.tsx index f1f67f7..e03f7b5 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,110 +1,148 @@ -"use client"; +"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, - CardHeader, - CardTitle, -} from "@/components/ui/card" +import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" -import { AlertCircle } from "lucide-react" - -import { - Alert, - AlertDescription, - AlertTitle, -} from "@/components/ui/alert" - -import { useState, useEffect } from "react"; -import Cookies from "js-cookie"; -import { useRouter } from "next/navigation"; -import axios from "axios"; +import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert" export default function Home() { - const [username, setUsername] = useState(''); - const [password, setPassword] = useState(''); - const router = useRouter(); - const [error, setError] = useState(''); - const [errorVisible, setErrorVisible] = useState(false); + 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'); + const token = Cookies.get("token") if (token) { - router.push('/dashboard'); + router.push("/dashboard") } - }, [router]); + }, [router]) interface LoginResponse { - token: string; + token: string } const login = async () => { - try { - const response = await axios.post('/api/auth/login', { username, password }); - const { token } = response.data as LoginResponse; - Cookies.set('token', token); - router.push('/dashboard'); - } catch (error: any) { - setError(error.response.data.error); + 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

-
- - - Login - - Enter your email and password to login. - - - - {errorVisible && ( - <> -
- - - Error - - {error} - - -
- - )} - -
-
-
- - setUsername(e.target.value)} - /> -
-
-
- -
- setPassword(e.target.value)}/> -
- +
+
+
+
+
+
- - +

CoreControl

+

Sign in to access your dashboard

+
+ + + + Login + Enter your credentials to continue + + + + {errorVisible && ( + + + Authentication Error + {error} + + )} + +
+
+ +
+ + setUsername(e.target.value)} + onKeyDown={handleKeyDown} + required + /> +
+
+ +
+
+ +
+
+ + setPassword(e.target.value)} + onKeyDown={handleKeyDown} + required + /> +
+
+
+
+ + + + +
+
) }