From b69a4b4976fa5bbb7f98d0b29e517dc821c37ca0 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 23 Feb 2026 12:41:15 +0800 Subject: [PATCH] fix: keep auth credentials when auth settings submit empty fields --- app/api/auth-settings/route.ts | 61 +++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/app/api/auth-settings/route.ts b/app/api/auth-settings/route.ts index 8d211dc04..f5b8891c8 100644 --- a/app/api/auth-settings/route.ts +++ b/app/api/auth-settings/route.ts @@ -112,6 +112,21 @@ const updateSchema = z.object({ resendFromEmail: z.string().optional(), }); +function resolveKeepExistingOnEmpty(params: { + incoming?: string; + existing?: string | null; + defaultValue?: string; +}) { + if (params.incoming === undefined) { + return params.existing ?? params.defaultValue ?? ""; + } + const trimmed = params.incoming.trim(); + if (trimmed.length === 0) { + return params.existing ?? params.defaultValue ?? ""; + } + return trimmed; +} + // PUT /api/auth-settings - 更新认证配置(仅管理员) export async function PUT(request: NextRequest) { try { @@ -136,21 +151,37 @@ export async function PUT(request: NextRequest) { | { name: string; appKey: string; appSecret: string } | undefined; - const resolvedAppKey = - data.dingTalkAppKey ?? primaryOrg?.appKey ?? existing?.dingTalkAppKey ?? null; - const resolvedAppSecret = - data.dingTalkAppSecret ?? primaryOrg?.appSecret ?? existing?.dingTalkAppSecret ?? null; - const resolvedGoogleClientId = - data.googleClientId ?? existing?.googleClientId ?? ""; - const resolvedGoogleClientSecret = - data.googleClientSecret ?? existing?.googleClientSecret ?? ""; - const resolvedAuthUrl = data.authUrl ?? existing?.authUrl ?? ""; - const resolvedResendApiKey = String( - data.resendApiKey ?? existing?.resendApiKey ?? "" - ).trim(); - const resolvedResendFromEmail = String( - data.resendFromEmail ?? existing?.resendFromEmail ?? "" - ).trim(); + const resolvedAppKey = resolveKeepExistingOnEmpty({ + incoming: data.dingTalkAppKey, + existing: primaryOrg?.appKey ?? existing?.dingTalkAppKey ?? null, + defaultValue: "", + }); + const resolvedAppSecret = resolveKeepExistingOnEmpty({ + incoming: data.dingTalkAppSecret, + existing: primaryOrg?.appSecret ?? existing?.dingTalkAppSecret ?? null, + defaultValue: "", + }); + const resolvedGoogleClientId = resolveKeepExistingOnEmpty({ + incoming: data.googleClientId, + existing: existing?.googleClientId, + defaultValue: "", + }); + const resolvedGoogleClientSecret = resolveKeepExistingOnEmpty({ + incoming: data.googleClientSecret, + existing: existing?.googleClientSecret, + defaultValue: "", + }); + const resolvedAuthUrl = data.authUrl?.trim() ?? existing?.authUrl ?? ""; + const resolvedResendApiKey = resolveKeepExistingOnEmpty({ + incoming: data.resendApiKey, + existing: existing?.resendApiKey, + defaultValue: "", + }); + const resolvedResendFromEmail = resolveKeepExistingOnEmpty({ + incoming: data.resendFromEmail, + existing: existing?.resendFromEmail, + defaultValue: "", + }); if (existing) { // 更新现有配置