2025-05-17 15:45:02 +02:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import Image from "next/image"
|
2025-05-17 17:12:43 +02:00
|
|
|
import { useState } from "react";
|
|
|
|
|
import { useRouter } from "next/navigation";
|
|
|
|
|
import axios from "axios";
|
|
|
|
|
import Cookies from "js-cookie";
|
|
|
|
|
import ErrorToast from "@/components/Error";
|
2025-05-17 15:45:02 +02:00
|
|
|
|
|
|
|
|
export default function LoginPage() {
|
2025-05-17 17:12:43 +02:00
|
|
|
const [email, setEmail] = useState("");
|
|
|
|
|
const [password, setPassword] = useState("");
|
|
|
|
|
const [remember, setRemember] = useState(false);
|
|
|
|
|
|
|
|
|
|
const [error, setError] = useState("");
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
|
|
const login = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const response = await axios.post("/api/user/login", { email, password, remember });
|
|
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
const token = response.data.token;
|
|
|
|
|
if (token) {
|
|
|
|
|
Cookies.set("token", token);
|
|
|
|
|
router.push("/dashboard");
|
|
|
|
|
} else {
|
|
|
|
|
setError("No login token received");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
setError(error.response.data.error);
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-05-17 15:45:02 +02:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="min-h-screen flex flex-col lg:flex-row">
|
|
|
|
|
<div className="flex-1 flex items-center justify-center p-6 lg:p-10">
|
2025-05-17 17:12:43 +02:00
|
|
|
<div className="w-full max-w-md space-y-8 bg">
|
2025-05-17 15:45:02 +02:00
|
|
|
<div className="text-center">
|
|
|
|
|
<h1 className="text-3xl font-bold">Welcome back</h1>
|
|
|
|
|
<p className="mt-2 text-gray-600">Please login with your account</p>
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-05-17 17:12:43 +02:00
|
|
|
<div className="mt-8 space-y-6">
|
2025-05-17 15:45:02 +02:00
|
|
|
<div className="form-control w-full">
|
|
|
|
|
<label className="label">
|
|
|
|
|
<span className="label-text">Email</span>
|
|
|
|
|
</label>
|
2025-05-17 17:12:43 +02:00
|
|
|
<input type="email" placeholder="email@example.com" className="input input-bordered w-full" required value={email} onChange={(e) => setEmail(e.target.value)} />
|
2025-05-17 15:45:02 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="form-control w-full">
|
|
|
|
|
<label className="label">
|
|
|
|
|
<span className="label-text">Password</span>
|
|
|
|
|
</label>
|
2025-05-17 17:12:43 +02:00
|
|
|
<input type="password" placeholder="Your password" className="input input-bordered w-full" required value={password} onChange={(e) => setPassword(e.target.value)} />
|
2025-05-17 15:45:02 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="form-control">
|
|
|
|
|
<label className="label cursor-pointer justify-start gap-2">
|
2025-05-17 17:12:43 +02:00
|
|
|
<input type="checkbox" className="checkbox checkbox-primary" checked={remember} onChange={(e) => setRemember(e.target.checked)} />
|
2025-05-17 15:45:02 +02:00
|
|
|
<span className="label-text">Stay logged in</span>
|
|
|
|
|
</label>
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-05-17 17:12:43 +02:00
|
|
|
<button className="btn btn-primary w-full" onClick={login}>Login</button>
|
|
|
|
|
</div>
|
2025-05-17 15:45:02 +02:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="flex-1 bg-base-200 hidden lg:block">
|
|
|
|
|
<div className="h-full w-full relative">
|
|
|
|
|
<Image
|
|
|
|
|
src="/login_wallpaper.jpg"
|
|
|
|
|
alt="Login Illustration"
|
|
|
|
|
fill
|
|
|
|
|
className="object-cover"
|
|
|
|
|
priority
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="h-48 bg-base-200 relative lg:hidden">
|
|
|
|
|
<Image
|
|
|
|
|
src="/login_wallpaper.jpg"
|
|
|
|
|
alt="Login Illustration"
|
|
|
|
|
fill
|
|
|
|
|
className="object-cover"
|
|
|
|
|
priority
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2025-05-17 17:12:43 +02:00
|
|
|
|
|
|
|
|
<ErrorToast message={error} show={error !== ""} onClose={() => setError("")} />
|
2025-05-17 15:45:02 +02:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|