89 lines
2.6 KiB
TypeScript
89 lines
2.6 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
|
|
export default function LoginPage() {
|
|
const router = useRouter();
|
|
const [email, setEmail] = useState("");
|
|
const [password, setPassword] = useState("");
|
|
const [error, setError] = useState("");
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
useEffect(() => {
|
|
fetch("/api/v1/auth/me", { credentials: "include" })
|
|
.then((r) => {
|
|
if (r.ok) router.push("/dashboard");
|
|
})
|
|
.catch(() => {});
|
|
}, [router]);
|
|
|
|
async function handleSubmit(e: React.FormEvent) {
|
|
e.preventDefault();
|
|
setLoading(true);
|
|
setError("");
|
|
try {
|
|
const res = await fetch("/api/v1/auth/login", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
credentials: "include",
|
|
body: JSON.stringify({ email, password }),
|
|
});
|
|
if (!res.ok) {
|
|
const text = await res.text();
|
|
let message = "Inloggen mislukt";
|
|
try {
|
|
const data = JSON.parse(text);
|
|
message = data.error?.message ?? message;
|
|
} catch {
|
|
message = text.slice(0, 120) || `Inloggen mislukt (HTTP ${res.status})`;
|
|
}
|
|
throw new Error(message);
|
|
}
|
|
window.location.assign("/dashboard");
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : "Inloggen mislukt");
|
|
setLoading(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="login-container">
|
|
<div className="login-box">
|
|
<div className="login-brand">
|
|
<span className="mark">MC</span>
|
|
<div>
|
|
<h1>Media Cluster</h1>
|
|
</div>
|
|
</div>
|
|
<p className="login-sub">Authenticated access // ops</p>
|
|
{error && <div className="error">{error}</div>}
|
|
<form onSubmit={handleSubmit}>
|
|
<div className="form-group">
|
|
<label>E-mail</label>
|
|
<input
|
|
type="email"
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
required
|
|
autoComplete="username"
|
|
/>
|
|
</div>
|
|
<div className="form-group">
|
|
<label>Wachtwoord</label>
|
|
<input
|
|
type="password"
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
required
|
|
autoComplete="current-password"
|
|
/>
|
|
</div>
|
|
<button type="submit" disabled={loading} style={{ width: "100%" }}>
|
|
{loading ? "Bezig..." : "Inloggen"}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|