fix: keep auth credentials when auth settings submit empty fields
This commit is contained in:
parent
aa1ec8bd43
commit
b69a4b4976
@ -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) {
|
||||
// 更新现有配置
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user