This commit is contained in:
2026-02-26 11:07:10 +08:00
parent 85edf0d847
commit eb68553baf
9 changed files with 329 additions and 168 deletions
-7
View File
@@ -22,13 +22,6 @@ export async function POST(req: Request) {
}
const initToken = process.env.INIT_SETUP_TOKEN;
const isProduction = process.env.NODE_ENV === "production";
if (isProduction && !initToken) {
return NextResponse.json(
{ error: "Server misconfigured: INIT_SETUP_TOKEN is required in production." },
{ status: 500 }
);
}
if (initToken) {
const providedToken = req.headers.get("x-init-token");
+15
View File
@@ -0,0 +1,15 @@
import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
export async function GET() {
try {
const settings = await prisma.globalSettings.findUnique({
where: { id: "default" },
select: { id: true },
});
return NextResponse.json({ initialized: Boolean(settings) });
} catch {
return NextResponse.json({ initialized: false, error: "Failed to check settings status" }, { status: 500 });
}
}
+155 -45
View File
@@ -1,19 +1,42 @@
"use client";
import { useState } from "react";
import { useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import { Lock, Sparkles } from "lucide-react";
export default function LoginPage() {
const [password, setPassword] = useState("");
const [initPassword, setInitPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [error, setError] = useState("");
const [isInitialized, setIsInitialized] = useState<boolean | null>(null);
const [isSubmitting, setIsSubmitting] = useState(false);
const [rememberMe, setRememberMe] = useState(false);
const [duration, setDuration] = useState("1");
const router = useRouter();
useEffect(() => {
const loadStatus = async () => {
try {
const res = await fetch("/api/settings/status", { cache: "no-store" });
if (!res.ok) {
setIsInitialized(true);
return;
}
const data = await res.json();
setIsInitialized(Boolean(data.initialized));
} catch {
setIsInitialized(true);
}
};
loadStatus();
}, []);
const handleLogin = async (e: React.FormEvent) => {
e.preventDefault();
if (isSubmitting) return;
setError("");
setIsSubmitting(true);
try {
const res = await fetch("/api/auth/login", {
@@ -34,9 +57,72 @@ export default function LoginPage() {
}
} catch {
setError("发生错误,请重试");
} finally {
setIsSubmitting(false);
}
};
const handleInitialize = async (e: React.FormEvent) => {
e.preventDefault();
if (isSubmitting) return;
setError("");
if (initPassword.length < 6) {
setError("密码至少 6 位");
return;
}
if (initPassword !== confirmPassword) {
setError("两次输入的密码不一致");
return;
}
setIsSubmitting(true);
try {
const initRes = await fetch("/api/settings/init", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ password: initPassword }),
});
const initData = await initRes.json().catch(() => ({}));
if (!initRes.ok) {
setError(initData.error || "初始化失败");
return;
}
const loginRes = await fetch("/api/auth/login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
password: initPassword,
rememberMe: false,
durationDays: 1,
}),
});
const loginData = await loginRes.json().catch(() => ({}));
if (!loginRes.ok) {
setError(loginData.error || "初始化成功,但自动登录失败,请手动登录");
setIsInitialized(true);
setPassword(initPassword);
return;
}
router.push("/");
} catch {
setError("初始化失败,请重试");
} finally {
setIsSubmitting(false);
}
};
if (isInitialized === null) {
return (
<div className="min-h-screen flex items-center justify-center bg-background p-4">
<div className="ui-card ui-enter w-full max-w-md p-8 text-center text-muted-foreground">...</div>
</div>
);
}
return (
<div className="min-h-screen flex items-center justify-center bg-background p-4">
<div className="ui-card ui-enter w-full max-w-md space-y-8 p-8 shadow-xl">
@@ -44,61 +130,85 @@ export default function LoginPage() {
<div className="ui-float inline-flex items-center justify-center w-16 h-16 bg-primary rounded-2xl text-primary-foreground mb-4 shadow-sm">
<Sparkles size={32} />
</div>
<h1 className="text-3xl font-bold tracking-tight"></h1>
<p className="text-muted-foreground">访</p>
<h1 className="text-3xl font-bold tracking-tight">{isInitialized ? "欢迎回来" : "首次初始化"}</h1>
<p className="text-muted-foreground">{isInitialized ? "请输入访问密码以继续。" : "请先设置访问密码,完成后将自动登录。"}</p>
</div>
<form onSubmit={handleLogin} className="space-y-4">
<form onSubmit={isInitialized ? handleLogin : handleInitialize} className="space-y-4">
<div className="space-y-2">
<div className="relative">
<Lock className="absolute left-3 top-1/2 -translate-y-1/2 text-muted-foreground" size={18} />
<input
type="password"
placeholder="访问密码"
value={password}
onChange={(e) => setPassword(e.target.value)}
className="ui-input pl-10 pr-4 py-3"
required
/>
</div>
{isInitialized ? (
<div className="relative">
<Lock className="absolute left-3 top-1/2 -translate-y-1/2 text-muted-foreground" size={18} />
<input
type="password"
placeholder="访问密码"
value={password}
onChange={(e) => setPassword(e.target.value)}
className="ui-input pl-10 pr-4 py-3"
required
/>
</div>
) : (
<>
<div className="relative">
<Lock className="absolute left-3 top-1/2 -translate-y-1/2 text-muted-foreground" size={18} />
<input
type="password"
placeholder="设置访问密码(至少6位)"
value={initPassword}
onChange={(e) => setInitPassword(e.target.value)}
className="ui-input pl-10 pr-4 py-3"
required
/>
</div>
<div className="relative">
<Lock className="absolute left-3 top-1/2 -translate-y-1/2 text-muted-foreground" size={18} />
<input
type="password"
placeholder="确认访问密码"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
className="ui-input pl-10 pr-4 py-3"
required
/>
</div>
</>
)}
{error && <p className="text-sm text-destructive font-medium">{error}</p>}
</div>
<div className="flex items-center justify-between text-sm px-1">
<label className="flex items-center gap-2 cursor-pointer text-muted-foreground hover:text-foreground transition-colors select-none">
<input
type="checkbox"
checked={rememberMe}
onChange={(e) => setRememberMe(e.target.checked)}
className="w-4 h-4 rounded border-input bg-background/50 text-primary focus:ring-primary/50"
/>
</label>
{isInitialized && (
<div className="flex items-center justify-between text-sm px-1">
<label className="flex items-center gap-2 cursor-pointer text-muted-foreground hover:text-foreground transition-colors select-none">
<input
type="checkbox"
checked={rememberMe}
onChange={(e) => setRememberMe(e.target.checked)}
className="w-4 h-4 rounded border-input bg-background/50 text-primary focus:ring-primary/50"
/>
</label>
{rememberMe && (
<select
value={duration}
onChange={(e) => setDuration(e.target.value)}
className="ui-select w-auto py-1 text-xs"
>
<option value="1">1</option>
<option value="7">7</option>
<option value="30">30</option>
</select>
)}
</div>
{rememberMe && (
<select
value={duration}
onChange={(e) => setDuration(e.target.value)}
className="ui-select w-auto py-1 text-xs"
>
<option value="1">1</option>
<option value="7">7</option>
<option value="30">30</option>
</select>
)}
</div>
)}
<button
type="submit"
className="ui-btn-primary w-full py-3 font-semibold shadow-md"
>
<button type="submit" disabled={isSubmitting} className="ui-btn-primary w-full py-3 font-semibold shadow-md">
{isSubmitting ? "处理中..." : isInitialized ? "登录" : "完成初始化"}
</button>
</form>
<div className="text-center text-xs text-muted-foreground">
NoteAI -
</div>
<div className="text-center text-xs text-muted-foreground">NoteAI - </div>
</div>
</div>
);
+35 -24
View File
@@ -293,53 +293,63 @@ export default function Home() {
editable={!activePage.isLocked}
/>
<section className="ui-enter mt-6 space-y-3 md:mt-8 md:space-y-4">
<div className="ui-card p-3 md:p-4">
<h3 className="text-sm font-semibold text-foreground"></h3>
<p className="mt-1 text-xs text-muted-foreground">使 `[[页面名]]` `Ctrl/Cmd + 点击` </p>
<div className="mt-3 flex flex-wrap gap-2">
<section className="ui-enter mt-6 space-y-3 md:mt-7">
<div className="grid gap-2 md:grid-cols-3">
<div className="ui-card p-2.5 md:p-3">
<div className="flex items-center justify-between">
<h3 className="text-sm font-semibold text-foreground"></h3>
<span className="rounded bg-muted px-1.5 py-0.5 text-[10px] text-muted-foreground">{outgoingLinks.length}</span>
</div>
<p className="mt-1 text-[11px] text-muted-foreground"> `[[页面名]]` </p>
<div className="mt-2 flex flex-wrap gap-1.5">
{outgoingLinks.length > 0 ? (
outgoingLinks.map((item) => (
<button
key={item.id}
onClick={() => setActivePageId(item.id)}
className="rounded-md border border-border/70 bg-muted/40 px-2.5 py-1 text-xs text-foreground transition-colors hover:bg-accent"
className="rounded-md border border-border/70 bg-muted/40 px-2 py-0.5 text-[11px] text-foreground transition-colors hover:bg-accent"
>
{item.icon ? `${item.icon} ` : ""}
{item.title}
</button>
))
) : (
<span className="text-xs text-muted-foreground"></span>
<span className="text-[11px] text-muted-foreground"></span>
)}
</div>
</div>
</div>
<div className="ui-card p-3 md:p-4">
<h3 className="text-sm font-semibold text-foreground"></h3>
<p className="mt-1 text-xs text-muted-foreground"></p>
<div className="mt-3 flex flex-wrap gap-2">
<div className="ui-card p-2.5 md:p-3">
<div className="flex items-center justify-between">
<h3 className="text-sm font-semibold text-foreground"></h3>
<span className="rounded bg-muted px-1.5 py-0.5 text-[10px] text-muted-foreground">{backlinks.length}</span>
</div>
<p className="mt-1 text-[11px] text-muted-foreground"></p>
<div className="mt-2 flex flex-wrap gap-1.5">
{backlinks.length > 0 ? (
backlinks.map((item) => (
<button
key={item.id}
onClick={() => setActivePageId(item.id)}
className="rounded-md border border-border/70 bg-muted/40 px-2.5 py-1 text-xs text-foreground transition-colors hover:bg-accent"
className="rounded-md border border-border/70 bg-muted/40 px-2 py-0.5 text-[11px] text-foreground transition-colors hover:bg-accent"
>
{item.icon ? `${item.icon} ` : ""}
{item.title}
</button>
))
) : (
<span className="text-xs text-muted-foreground"></span>
<span className="text-[11px] text-muted-foreground"></span>
)}
</div>
</div>
</div>
<div className="ui-card p-3 md:p-4">
<h3 className="text-sm font-semibold text-foreground"></h3>
<p className="mt-1 text-xs text-muted-foreground"> `[[页面名]]` </p>
<div className="mt-3 flex flex-wrap gap-2">
<div className="ui-card p-2.5 md:p-3">
<div className="flex items-center justify-between">
<h3 className="text-sm font-semibold text-foreground"></h3>
<span className="rounded bg-muted px-1.5 py-0.5 text-[10px] text-muted-foreground">{unresolvedLinks.length}</span>
</div>
<p className="mt-1 text-[11px] text-muted-foreground"></p>
<div className="mt-2 flex flex-wrap gap-1.5">
{unresolvedLinks.length > 0 ? (
unresolvedLinks.map((title) => (
<button
@@ -347,19 +357,20 @@ export default function Home() {
onClick={async () => {
await addPage(null, "file", { title, content: "" });
}}
className="rounded-md border border-dashed border-border bg-muted/30 px-2.5 py-1 text-xs text-foreground transition-colors hover:bg-accent"
className="rounded-md border border-dashed border-border bg-muted/30 px-2 py-0.5 text-[11px] text-foreground transition-colors hover:bg-accent"
title={`创建页面:${title}`}
>
+ {title}
</button>
))
) : (
<span className="text-xs text-muted-foreground"></span>
<span className="text-[11px] text-muted-foreground"></span>
)}
</div>
</div>
</div>
<div className="ui-card p-3 md:p-4">
<div className="ui-card p-2.5 md:p-3">
<button
onClick={() => setIsHistoryOpen((v) => !v)}
className="flex w-full items-center justify-between rounded-md px-1 py-1 text-left transition-colors hover:bg-muted/45"
@@ -373,9 +384,9 @@ export default function Home() {
</div>
<ChevronDown size={15} className={cn("text-muted-foreground transition-transform", isHistoryOpen && "rotate-180")} />
</button>
<p className="mt-1 text-xs text-muted-foreground"></p>
<p className="mt-1 text-[11px] text-muted-foreground"></p>
{isHistoryOpen && (
<div className="mt-3 space-y-3">
<div className="mt-2 space-y-2.5">
{localHistory.length > 0 ? (
<>
<div className="rounded-lg border border-border/60 bg-muted/20 p-2">