From 68cb005dd338f740320d9f2f4c6d3890ac827cd2 Mon Sep 17 00:00:00 2001 From: Dream Hunter Date: Sat, 9 Sep 2023 17:00:20 +0800 Subject: [PATCH] fix: do not show auth when input wrong auth code (#23) --- frontend/src/api/index.js | 4 ++-- frontend/src/store/index.js | 2 +- worker/src/router.js | 10 +++++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/frontend/src/api/index.js b/frontend/src/api/index.js index 6c4cdf7d..0822feac 100644 --- a/frontend/src/api/index.js +++ b/frontend/src/api/index.js @@ -33,7 +33,7 @@ const getOpenSettings = async (message) => { const res = await api.fetch("/open_api/settings"); openSettings.value = { prefix: res["prefix"] || "", - auth: res["auth"] || false, + needAuth: res["needAuth"] || false, domains: res["domains"].map((domain) => { return { label: domain, @@ -41,7 +41,7 @@ const getOpenSettings = async (message) => { } }) }; - if (openSettings.value.auth && !auth.value) { + if (openSettings.value.needAuth) { showAuth.value = true; } } catch (error) { diff --git a/frontend/src/store/index.js b/frontend/src/store/index.js index 6e4f3423..604c5e4f 100644 --- a/frontend/src/store/index.js +++ b/frontend/src/store/index.js @@ -6,7 +6,7 @@ export const useGlobalState = createGlobalState( const loading = ref(false); const openSettings = ref({ prefix: '', - auth: false, + needAuth: false, domains: [{ label: 'test.com', value: 'test.com' diff --git a/worker/src/router.js b/worker/src/router.js index 4e7d7d1b..7b8dfca1 100644 --- a/worker/src/router.js +++ b/worker/src/router.js @@ -19,12 +19,16 @@ api.get('/api/settings', async (c) => { }) api.get('/open_api/settings', async (c) => { + // check header x-custom-auth + let needAuth = false; + if (c.env.PASSWORDS && c.env.PASSWORDS.length > 0) { + const auth = c.req.headers.get("x-custom-auth"); + needAuth = !c.env.PASSWORDS.includes(auth); + } return c.json({ "prefix": c.env.PREFIX, "domains": c.env.DOMAINS, - "auth": ( - c.env.PASSWORDS && c.env.PASSWORDS.length > 0 - ) ? true : false, + "needAuth": needAuth, }); })