From 987cbdcda150e95c736d0075ede7b41ff2412342 Mon Sep 17 00:00:00 2001 From: Chris Bisset Date: Sun, 23 Mar 2025 00:04:14 +0000 Subject: [PATCH] clean up async functions --- .../settings/server-settings-functions.svelte | 36 +++++++++---------- .../settings/server-settings.svelte | 3 +- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/lib/components/settings/server-settings-functions.svelte b/src/lib/components/settings/server-settings-functions.svelte index 5629539..729163e 100644 --- a/src/lib/components/settings/server-settings-functions.svelte +++ b/src/lib/components/settings/server-settings-functions.svelte @@ -47,30 +47,30 @@ } } - export function rotateAPIKey() { - appSettings.apiKeyList.forEach((key) => { - // select the current key being used + export async function rotateAPIKey() { + for (const key of appSettings.apiKeyList) { + // select the current key being used in the app settings if (persistentAppSettings.headscaleAPIKey.startsWith(key.prefix)) { let currentKey = key; + + // generate a new expiration time 90 days in the future let newExpiration = new Date(); newExpiration.setDate(newExpiration.getDate() + 90); - // create a new API key with the new new expiration, set it as the current API key, - // and then expire the previous API key - - createNewAPIKey(newExpiration).then((apiKey) => { - if (apiKey == undefined) { - throw new Error('expecting API key string, string was undefined'); - } - persistentAppSettings.headscaleAPIKey = apiKey; - expireAPIKey(currentKey.prefix).then(() => { - getAPIKeys().then(() => { - // console.log(appSettings.apiKeyList); - }); - }); - }); + // create a new API key with the new expiration + let apiKey = await createNewAPIKey(newExpiration); + // The above should always return a value + if (apiKey == undefined) { + throw new Error('expecting API key string, string was undefined'); + } + // Set the new key as the current key in the persistent settings + persistentAppSettings.headscaleAPIKey = apiKey; + // Expire the previously current key + await expireAPIKey(currentKey.prefix); + // Get keys again to make sure it all worked + await getAPIKeys(); } - }); + } } export async function createNewAPIKey(expireDate: Date) { diff --git a/src/lib/components/settings/server-settings.svelte b/src/lib/components/settings/server-settings.svelte index b968843..94723e7 100644 --- a/src/lib/components/settings/server-settings.svelte +++ b/src/lib/components/settings/server-settings.svelte @@ -9,8 +9,7 @@ function rotateAPIKeyClick() { rotateButtonDisabled = true; - rotateAPIKey(); - rotateButtonDisabled = false; + rotateAPIKey().then(() => {rotateButtonDisabled = false}); }