93 lines
3.1 KiB
TypeScript
93 lines
3.1 KiB
TypeScript
"use client";
|
|
import { fetchData } from "@/lib/data-service";
|
|
|
|
import { useState } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import Image from "next/image";
|
|
import Link from "next/link";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Checkbox } from "@/components/ui/checkbox";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Label } from "@/components/ui/label";
|
|
|
|
export default function LoginPage() {
|
|
const [username, setUsername] = useState(""); // ✅ Fixed incorrect state name
|
|
const [password, setPassword] = useState("");
|
|
const [error, setError] = useState("");
|
|
const router = useRouter();
|
|
|
|
async function handleLogin(e: React.FormEvent) {
|
|
e.preventDefault();
|
|
setError("");
|
|
|
|
const res = await fetchData("http://localhost:3001/api/auth/login", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ username, password }),
|
|
});
|
|
|
|
if (res.token) {
|
|
document.cookie = `Authorization=${res.token}; path=/; Secure; SameSite=Strict; max-age=604800`;
|
|
router.push("/dashboard");
|
|
} else {
|
|
const data = await res.json();
|
|
setError(data.error || "Invalid credentials");
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="flex items-center justify-center min-h-screen bg-gray-100 dark:bg-[#0F0F12]">
|
|
<div className="w-full max-w-md p-8 space-y-8 bg-white dark:bg-[#1F1F23] rounded-xl shadow-lg">
|
|
<div className="text-center">
|
|
<h2 className="mt-6 text-3xl font-bold text-gray-900 dark:text-white">Welcome back</h2>
|
|
<p className="mt-2 text-sm text-gray-600 dark:text-gray-400">Please sign in to your account</p>
|
|
</div>
|
|
|
|
{error && <p className="text-red-500 text-sm text-center">{error}</p>} {/* ✅ Display login errors */}
|
|
|
|
<form className="mt-8 space-y-6" onSubmit={handleLogin}>
|
|
<div className="space-y-4">
|
|
<div>
|
|
<Label htmlFor="username">Username</Label> {/* ✅ Changed Email to Username */}
|
|
<Input
|
|
id="username"
|
|
name="username"
|
|
type="text"
|
|
autoComplete="username"
|
|
required
|
|
value={username}
|
|
onChange={(e) => setUsername(e.target.value)}
|
|
className="mt-1"
|
|
/>
|
|
</div>
|
|
<div>
|
|
<Label htmlFor="password">Password</Label>
|
|
<Input
|
|
id="password"
|
|
name="password"
|
|
type="password"
|
|
autoComplete="current-password"
|
|
required
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
className="mt-1"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<Button type="submit" className="w-full">
|
|
Sign in
|
|
</Button>
|
|
</form>
|
|
|
|
<p className="mt-10 text-sm text-center text-gray-600 dark:text-gray-400">
|
|
Don't have an account?{" "}
|
|
<Link href="/signup" className="text-blue-600 hover:underline dark:text-blue-400">
|
|
Sign up
|
|
</Link>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
);
|
|
} |