2025-04-11 13:37:41 +02:00
|
|
|
"use client";
|
|
|
|
|
|
2025-04-11 12:27:44 +02:00
|
|
|
import { Button } from "@/components/ui/button"
|
|
|
|
|
import {
|
|
|
|
|
Card,
|
|
|
|
|
CardContent,
|
|
|
|
|
CardDescription,
|
|
|
|
|
CardHeader,
|
|
|
|
|
CardTitle,
|
|
|
|
|
} from "@/components/ui/card"
|
|
|
|
|
import { Input } from "@/components/ui/input"
|
|
|
|
|
import { Label } from "@/components/ui/label"
|
2025-04-11 13:37:41 +02:00
|
|
|
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";
|
|
|
|
|
|
2025-04-11 12:27:44 +02:00
|
|
|
export default function Home() {
|
2025-04-11 13:37:41 +02:00
|
|
|
const [username, setUsername] = useState('');
|
|
|
|
|
const [password, setPassword] = useState('');
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
const [error, setError] = useState('');
|
|
|
|
|
const [errorVisible, setErrorVisible] = useState(false);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const token = Cookies.get('token');
|
|
|
|
|
if (token) {
|
|
|
|
|
router.push('/dashboard');
|
|
|
|
|
}
|
|
|
|
|
}, [router]);
|
|
|
|
|
|
|
|
|
|
interface LoginResponse {
|
|
|
|
|
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);
|
|
|
|
|
setErrorVisible(true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-11 12:27:44 +02:00
|
|
|
return (
|
|
|
|
|
<div className="flex flex-col min-h-screen items-center justify-center gap-6 ">
|
2025-04-15 14:59:23 +02:00
|
|
|
<div className="flex flex-col items-center md:pb-4">
|
2025-04-15 15:01:11 +02:00
|
|
|
<h1 className="text-4xl md:text-5xl lg:text-8xl font-bold">CoreControl</h1>
|
2025-04-15 14:59:23 +02:00
|
|
|
</div>
|
2025-04-15 15:01:11 +02:00
|
|
|
<Card className="w-5/6 md:w-2/3 lg:w-1/3">
|
2025-04-11 12:27:44 +02:00
|
|
|
<CardHeader>
|
|
|
|
|
<CardTitle className="text-2xl">Login</CardTitle>
|
|
|
|
|
<CardDescription>
|
2025-04-14 20:59:59 +02:00
|
|
|
Enter your email and password to login.
|
2025-04-11 12:27:44 +02:00
|
|
|
</CardDescription>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent>
|
2025-04-11 13:37:41 +02:00
|
|
|
{errorVisible && (
|
|
|
|
|
<>
|
|
|
|
|
<div className="pb-4">
|
|
|
|
|
<Alert variant="destructive">
|
|
|
|
|
<AlertCircle className="h-4 w-4" />
|
|
|
|
|
<AlertTitle>Error</AlertTitle>
|
|
|
|
|
<AlertDescription>
|
|
|
|
|
{error}
|
|
|
|
|
</AlertDescription>
|
|
|
|
|
</Alert>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<div>
|
2025-04-11 12:27:44 +02:00
|
|
|
<div className="flex flex-col gap-6">
|
|
|
|
|
<div className="grid gap-2">
|
|
|
|
|
<Label htmlFor="email">Email</Label>
|
|
|
|
|
<Input
|
|
|
|
|
id="email"
|
|
|
|
|
type="email"
|
|
|
|
|
placeholder="mail@example.com"
|
|
|
|
|
required
|
2025-04-11 13:37:41 +02:00
|
|
|
onChange={(e) => setUsername(e.target.value)}
|
2025-04-11 12:27:44 +02:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="grid gap-2">
|
|
|
|
|
<div className="flex items-center">
|
|
|
|
|
<Label htmlFor="password">Password</Label>
|
|
|
|
|
</div>
|
2025-04-11 13:37:41 +02:00
|
|
|
<Input id="password" type="password" required placeholder="* * * * * * *" onChange={(e) => setPassword(e.target.value)}/>
|
2025-04-11 12:27:44 +02:00
|
|
|
</div>
|
2025-04-11 13:37:41 +02:00
|
|
|
<Button className="w-full" onClick={login}>
|
2025-04-11 12:27:44 +02:00
|
|
|
Login
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
2025-04-11 13:37:41 +02:00
|
|
|
</div>
|
2025-04-11 12:27:44 +02:00
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|