fix: keep auth credentials when auth settings submit empty fields
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled

This commit is contained in:
Your Name 2026-02-23 12:41:15 +08:00
parent aa1ec8bd43
commit b69a4b4976

View File

@ -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) {
// 更新现有配置