feat: add pause/resume functionality for API keys (#158)

- Add isActive field to API key schema with migration
- Implement PUT /api/keys/[id] endpoint for toggle
- Update validation to reject paused keys (403)
- Add UI toggle controls with confirmation
- Ensure cloud sync preserves pause state
This commit is contained in:
Thiên Toán
2026-02-20 15:07:12 +07:00
committed by GitHub
parent 806bd4ae14
commit 73388a02a1
4 changed files with 119 additions and 9 deletions

View File

@@ -115,6 +115,16 @@ function ensureDbShape(data) {
}
}
}
// Migrate existing API keys to have isActive
if (key === "apiKeys" && Array.isArray(next.apiKeys)) {
for (const apiKey of next.apiKeys) {
if (apiKey.isActive === undefined || apiKey.isActive === null) {
apiKey.isActive = true;
changed = true;
}
}
}
}
return { data: next, changed };
@@ -649,6 +659,7 @@ export async function createApiKey(name, machineId) {
name: name,
key: result.key,
machineId: machineId,
isActive: true,
createdAt: now,
};
@@ -673,12 +684,36 @@ export async function deleteApiKey(id) {
return true;
}
/**
* Get API key by ID
*/
export async function getApiKeyById(id) {
const db = await getDb();
return db.data.apiKeys.find(k => k.id === id) || null;
}
/**
* Update API key
*/
export async function updateApiKey(id, data) {
const db = await getDb();
const index = db.data.apiKeys.findIndex(k => k.id === id);
if (index === -1) return null;
db.data.apiKeys[index] = {
...db.data.apiKeys[index],
...data,
};
await db.write();
return db.data.apiKeys[index];
}
/**
* Validate API key
*/
export async function validateApiKey(key) {
const db = await getDb();
return db.data.apiKeys.some(k => k.key === key);
const found = db.data.apiKeys.find(k => k.key === key);
return found && found.isActive !== false;
}
// ============ Data Cleanup ============