Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/static/common/admin-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ async function requestApiKey(creds) {
return cachedApiKey;
}

async function ensureApiKey() {
async function ensureApiKey(forceRefresh = false) {
if (cachedApiKey && !forceRefresh) {
return cachedApiKey;
}
const creds = await getStoredAppKey();
if (!creds || !creds.password) {
window.location.href = '/login';
Expand Down
30 changes: 25 additions & 5 deletions app/static/keys/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,20 @@ function applyKeyLimitPreset(mode) {
q('limit-video').value = recommended.video;
}

async function loadKeys() {
async function loadKeys(retryOn401 = true) {
const body = q('keys-table-body');
if (body) body.innerHTML = '';
setLoading(true);
setEmptyState(false);
try {
const res = await fetch('/api/v1/admin/keys', { headers: buildAuthHeaders(apiKey) });
if (res.status === 401) return logout();
if (res.status === 401) {
if (retryOn401) {
apiKey = await ensureApiKey(true);
if (apiKey) return loadKeys(false);
}
return logout();
}
const payload = await parseJsonSafely(res);
if (!res.ok || payload?.success !== true) {
throw new Error(extractErrorMessage(payload, '加载失败'));
Expand Down Expand Up @@ -428,7 +434,12 @@ async function submitKeyModal() {
is_active: isActive,
}),
});
if (res.status === 401) return logout();
if (res.status === 401) {
apiKey = await ensureApiKey(true);
if (!apiKey) return logout();
setSubmitState(false);
return submitKeyModal();
}
const payload = await parseJsonSafely(res);
if (!res.ok || payload?.success !== true) {
throw new Error(extractErrorMessage(payload, '创建失败'));
Expand Down Expand Up @@ -457,7 +468,12 @@ async function submitKeyModal() {
limits,
}),
});
if (res.status === 401) return logout();
if (res.status === 401) {
apiKey = await ensureApiKey(true);
if (!apiKey) return logout();
setSubmitState(false);
return submitKeyModal();
}
const payload = await parseJsonSafely(res);
if (!res.ok || payload?.success !== true) {
throw new Error(extractErrorMessage(payload, '更新失败'));
Expand All @@ -484,7 +500,11 @@ async function deleteKey(row) {
headers: { ...buildAuthHeaders(apiKey), 'Content-Type': 'application/json' },
body: JSON.stringify({ key }),
});
if (res.status === 401) return logout();
if (res.status === 401) {
apiKey = await ensureApiKey(true);
if (!apiKey) return logout();
return deleteKey(row);
}
const payload = await parseJsonSafely(res);
if (!res.ok || payload?.success !== true) {
throw new Error(extractErrorMessage(payload, '删除失败'));
Expand Down