2025-04-15 16:14:10 +02:00
|
|
|
"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"
|
2025-04-11 13:37:41 +02:00
|
|
|
|
2025-04-11 12:27:44 +02:00
|
|
|
import { Button } from "@/components/ui/button"
|
2025-04-15 16:14:10 +02:00
|
|
|
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"
|
2025-04-11 12:27:44 +02:00
|
|
|
import { Input } from "@/components/ui/input"
|
|
|
|
|
import { Label } from "@/components/ui/label"
|
2025-04-15 16:14:10 +02:00
|
|
|
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
|
2025-04-11 13:37:41 +02:00
|
|
|
|
2025-04-11 12:27:44 +02:00
|
|
|
export default function Home() {
|
2025-04-15 16:14:10 +02:00
|
|
|
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)
|
2025-04-11 13:37:41 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-04-15 16:14:10 +02:00
|
|
|
const token = Cookies.get("token")
|
2025-04-11 13:37:41 +02:00
|
|
|
if (token) {
|
2025-04-15 16:14:10 +02:00
|
|
|
router.push("/dashboard")
|
2025-04-11 13:37:41 +02:00
|
|
|
}
|
2025-04-15 16:14:10 +02:00
|
|
|
}, [router])
|
2025-04-11 13:37:41 +02:00
|
|
|
|
|
|
|
|
interface LoginResponse {
|
2025-04-15 16:14:10 +02:00
|
|
|
token: string
|
2025-04-11 13:37:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const login = async () => {
|
2025-04-15 16:14:10 +02:00
|
|
|
if (!username || !password) {
|
|
|
|
|
setError("Please enter both email and password")
|
|
|
|
|
setErrorVisible(true)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-11 13:37:41 +02:00
|
|
|
try {
|
2025-04-15 16:14:10 +02:00
|
|
|
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")
|
2025-04-11 13:37:41 +02:00
|
|
|
} catch (error: any) {
|
2025-04-15 16:14:10 +02:00
|
|
|
setError(error.response?.data?.error || "Login failed. Please try again.")
|
2025-04-11 13:37:41 +02:00
|
|
|
setErrorVisible(true)
|
2025-04-15 16:14:10 +02:00
|
|
|
} finally {
|
|
|
|
|
setIsLoading(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleKeyDown = (e: React.KeyboardEvent) => {
|
|
|
|
|
if (e.key === "Enter") {
|
|
|
|
|
login()
|
2025-04-11 13:37:41 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-11 12:27:44 +02:00
|
|
|
return (
|
2025-04-15 16:14:10 +02:00
|
|
|
<div className="min-h-screen flex flex-col items-center justify-center bg-gradient-to-b from-background to-muted/30 p-4">
|
|
|
|
|
<div className="w-full max-w-md space-y-8">
|
|
|
|
|
<div className="text-center space-y-2">
|
|
|
|
|
<div className="flex justify-center">
|
|
|
|
|
<div className="h-16 w-16 rounded-full bg-primary/10 flex items-center justify-center">
|
|
|
|
|
<KeyRound className="h-8 w-8 text-primary" />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<h1 className="text-4xl font-bold tracking-tight text-foreground">CoreControl</h1>
|
|
|
|
|
<p className="text-muted-foreground">Sign in to access your dashboard</p>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<Card className="border-muted/40 shadow-lg">
|
|
|
|
|
<CardHeader className="space-y-1">
|
|
|
|
|
<CardTitle className="text-2xl font-semibold">Login</CardTitle>
|
|
|
|
|
<CardDescription>Enter your credentials to continue</CardDescription>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
|
|
|
|
|
<CardContent className="space-y-4">
|
|
|
|
|
{errorVisible && (
|
|
|
|
|
<Alert variant="destructive" className="animate-in fade-in-50 slide-in-from-top-5">
|
2025-04-11 13:37:41 +02:00
|
|
|
<AlertCircle className="h-4 w-4" />
|
2025-04-15 16:14:10 +02:00
|
|
|
<AlertTitle>Authentication Error</AlertTitle>
|
|
|
|
|
<AlertDescription>{error}</AlertDescription>
|
2025-04-11 13:37:41 +02:00
|
|
|
</Alert>
|
2025-04-15 16:14:10 +02:00
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<div className="space-y-4">
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<Label htmlFor="email" className="text-sm font-medium">
|
|
|
|
|
Email
|
|
|
|
|
</Label>
|
|
|
|
|
<div className="relative">
|
|
|
|
|
<Mail className="absolute left-3 top-2.5 h-5 w-5 text-muted-foreground" />
|
|
|
|
|
<Input
|
|
|
|
|
id="email"
|
|
|
|
|
type="email"
|
|
|
|
|
placeholder="mail@example.com"
|
|
|
|
|
className="pl-10"
|
|
|
|
|
value={username}
|
|
|
|
|
onChange={(e) => setUsername(e.target.value)}
|
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
|
required
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2025-04-11 12:27:44 +02:00
|
|
|
</div>
|
2025-04-15 16:14:10 +02:00
|
|
|
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<Label htmlFor="password" className="text-sm font-medium">
|
|
|
|
|
Password
|
|
|
|
|
</Label>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="relative">
|
|
|
|
|
<User className="absolute left-3 top-2.5 h-5 w-5 text-muted-foreground" />
|
|
|
|
|
<Input
|
|
|
|
|
id="password"
|
|
|
|
|
type="password"
|
|
|
|
|
placeholder="••••••••"
|
|
|
|
|
className="pl-10"
|
|
|
|
|
value={password}
|
|
|
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
|
required
|
|
|
|
|
/>
|
2025-04-11 12:27:44 +02:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-04-15 16:14:10 +02:00
|
|
|
</CardContent>
|
|
|
|
|
|
|
|
|
|
<CardFooter className="flex flex-col space-y-4">
|
|
|
|
|
<Button className="w-full" onClick={login} disabled={isLoading}>
|
|
|
|
|
{isLoading ? "Signing in..." : "Sign in"}
|
|
|
|
|
</Button>
|
|
|
|
|
</CardFooter>
|
|
|
|
|
</Card>
|
|
|
|
|
</div>
|
2025-04-11 12:27:44 +02:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|