9router/src/app/login/page.js

134 lines
4 KiB
JavaScript
Raw Normal View History

"use client";
2026-01-14 02:55:47 -05:00
import { useState, useEffect } from "react";
import { Card, Button, Input } from "@/shared/components";
import { useRouter } from "next/navigation";
export default function LoginPage() {
const [password, setPassword] = useState("");
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
2026-01-14 02:55:47 -05:00
const [hasPassword, setHasPassword] = useState(null);
const router = useRouter();
2026-01-14 02:55:47 -05:00
useEffect(() => {
async function checkAuth() {
2026-01-26 22:49:16 -05:00
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);
const baseUrl = typeof window !== "undefined" ? window.location.origin : "";
2026-01-26 22:49:16 -05:00
2026-01-14 02:55:47 -05:00
try {
2026-01-26 22:49:16 -05:00
const res = await fetch(`${baseUrl}/api/settings`, {
signal: controller.signal,
});
clearTimeout(timeoutId);
2026-01-14 02:55:47 -05:00
if (res.ok) {
const data = await res.json();
if (data.requireLogin === false) {
router.push("/dashboard");
router.refresh();
return;
}
2026-01-14 02:55:47 -05:00
if (!data.password) {
2026-01-26 22:49:16 -05:00
const loginRes = await fetch(`${baseUrl}/api/auth/login`, {
2026-01-14 02:55:47 -05:00
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ password: "123456" }),
});
if (loginRes.ok) {
router.push("/dashboard");
router.refresh();
return;
}
}
setHasPassword(!!data.password);
}
} catch (err) {
2026-01-26 22:49:16 -05:00
clearTimeout(timeoutId);
setHasPassword(true);
2026-01-14 02:55:47 -05:00
}
}
checkAuth();
2026-01-14 02:55:47 -05:00
}, [router]);
const handleLogin = async (e) => {
e.preventDefault();
setLoading(true);
setError("");
try {
const res = await fetch("/api/auth/login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ password }),
});
if (res.ok) {
router.push("/dashboard");
router.refresh();
} else {
const data = await res.json();
setError(data.error || "Invalid password");
}
} catch (err) {
setError("An error occurred. Please try again.");
} finally {
setLoading(false);
}
};
2026-01-14 02:55:47 -05:00
// Show loading state while checking password
if (hasPassword === null) {
return (
<div className="min-h-screen flex items-center justify-center bg-bg p-4">
<div className="text-center">
<div className="inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-primary"></div>
<p className="text-text-muted mt-4">Loading...</p>
</div>
</div>
);
}
return (
<div className="min-h-screen flex items-center justify-center bg-bg p-4">
<div className="w-full max-w-md">
<div className="text-center mb-8">
<h1 className="text-3xl font-bold text-primary mb-2">9Router</h1>
<p className="text-text-muted">Enter your password to access the dashboard</p>
</div>
<Card>
<form onSubmit={handleLogin} className="flex flex-col gap-4">
<div className="flex flex-col gap-2">
<label className="text-sm font-medium">Password</label>
<Input
type="password"
placeholder="Enter password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
autoFocus
/>
{error && <p className="text-xs text-red-500">{error}</p>}
</div>
<Button
type="submit"
variant="primary"
className="w-full"
loading={loading}
>
Login
</Button>
<p className="text-xs text-center text-text-muted mt-2">
Default password is <code className="bg-sidebar px-1 rounded">123456</code>
</p>
</form>
</Card>
</div>
</div>
);
}