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(), 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 - 更新认证配置(仅管理员) // PUT /api/auth-settings - 更新认证配置(仅管理员)
export async function PUT(request: NextRequest) { export async function PUT(request: NextRequest) {
try { try {
@ -136,21 +151,37 @@ export async function PUT(request: NextRequest) {
| { name: string; appKey: string; appSecret: string } | { name: string; appKey: string; appSecret: string }
| undefined; | undefined;
const resolvedAppKey = const resolvedAppKey = resolveKeepExistingOnEmpty({
data.dingTalkAppKey ?? primaryOrg?.appKey ?? existing?.dingTalkAppKey ?? null; incoming: data.dingTalkAppKey,
const resolvedAppSecret = existing: primaryOrg?.appKey ?? existing?.dingTalkAppKey ?? null,
data.dingTalkAppSecret ?? primaryOrg?.appSecret ?? existing?.dingTalkAppSecret ?? null; defaultValue: "",
const resolvedGoogleClientId = });
data.googleClientId ?? existing?.googleClientId ?? ""; const resolvedAppSecret = resolveKeepExistingOnEmpty({
const resolvedGoogleClientSecret = incoming: data.dingTalkAppSecret,
data.googleClientSecret ?? existing?.googleClientSecret ?? ""; existing: primaryOrg?.appSecret ?? existing?.dingTalkAppSecret ?? null,
const resolvedAuthUrl = data.authUrl ?? existing?.authUrl ?? ""; defaultValue: "",
const resolvedResendApiKey = String( });
data.resendApiKey ?? existing?.resendApiKey ?? "" const resolvedGoogleClientId = resolveKeepExistingOnEmpty({
).trim(); incoming: data.googleClientId,
const resolvedResendFromEmail = String( existing: existing?.googleClientId,
data.resendFromEmail ?? existing?.resendFromEmail ?? "" defaultValue: "",
).trim(); });
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) { if (existing) {
// 更新现有配置 // 更新现有配置