修改登录逻辑

This commit is contained in:
2026-07-06 15:54:27 +08:00
parent fe66794168
commit 53b1842de8
12 changed files with 198 additions and 28 deletions

View File

@@ -21,10 +21,6 @@ type PasswordTokenResponse = {
export async function POST(request: Request) {
try {
const config = getAuthRuntimeConfig();
if (!config.configured || !config.tokenUrl || !config.clientSecret || !config.sessionSecret) {
throw new PasswordLoginError(`认证配置不完整:${config.missing.join(", ") || "未知配置"}`, 500);
}
const body = await readJsonBody<{
username?: string;
password?: string;
@@ -33,7 +29,12 @@ export async function POST(request: Request) {
code?: string;
randomStr?: string;
next?: string;
authMode?: string;
}>(request);
const config = getAuthRuntimeConfig({ clientMode: body.authMode === "admin" ? "admin" : "default" });
if (!config.configured || !config.tokenUrl || !config.clientSecret || !config.sessionSecret) {
throw new PasswordLoginError(`认证配置不完整:${config.missing.join(", ") || "未知配置"}`, 500);
}
const username = body.username?.trim();
const password = body.password || "";
const code = body.code?.trim();

View File

@@ -0,0 +1,47 @@
import { redirect } from "next/navigation";
import { AuthLoginPanel } from "@/components/auth-login-panel";
import { getAuthRuntimeConfig, safeNextPath } from "@/lib/auth/config";
import { getOptionalAuthSession } from "@/lib/server/auth/current-user";
const errorMessages: Record<string, string> = {
auth_not_configured: "认证配置不完整,请先在服务器环境变量中配置 SSO。",
callback_failed: "登录回调处理失败,请重新登录。",
state_invalid: "登录状态已失效,请重新登录。"
};
export default async function AdminLoginPage({
searchParams
}: {
searchParams?: Promise<Record<string, string | string[] | undefined>>;
}) {
const params = await searchParams;
const next = safeNextPath(singleParam(params?.next));
const session = await getOptionalAuthSession();
if (session) redirect(next);
const config = getAuthRuntimeConfig({ clientMode: "admin" });
const errorCode = singleParam(params?.error);
const message = errorCode ? errorMessages[errorCode] || "登录失败,请重新登录。" : null;
return (
<AuthLoginPanel
next={next}
configured={config.configured}
message={message}
missing={!config.configured && config.required ? config.missing : []}
title="管理员登录"
submitLabel="管理员登录"
authMode="admin"
alternateHref={loginHref("/auth/login", next)}
alternateLabel="普通账号登录"
/>
);
}
function singleParam(value: string | string[] | undefined): string | undefined {
return Array.isArray(value) ? value[0] : value;
}
function loginHref(path: string, next: string): string {
return `${path}?next=${encodeURIComponent(next)}`;
}

View File

@@ -29,6 +29,8 @@ export default async function LoginPage({
configured={config.configured}
message={message}
missing={!config.configured && config.required ? config.missing : []}
alternateHref={loginHref("/auth/admin-login", next)}
alternateLabel="管理员登录"
/>
);
}
@@ -36,3 +38,7 @@ export default async function LoginPage({
function singleParam(value: string | string[] | undefined): string | undefined {
return Array.isArray(value) ? value[0] : value;
}
function loginHref(path: string, next: string): string {
return `${path}?next=${encodeURIComponent(next)}`;
}