From d99031b3b64382d70c4f245cc793ef8690121a54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B3=E5=85=83=E7=9A=93?= Date: Mon, 9 Jun 2025 14:10:08 +0800 Subject: [PATCH] Groq action actully works? --- components/app/windows/settings.vue | 23 ++++++++++++++---- server/api/user/submitGroqKey.ts | 36 +++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 server/api/user/submitGroqKey.ts diff --git a/components/app/windows/settings.vue b/components/app/windows/settings.vue index b5a4ee1..4dfb9e9 100644 --- a/components/app/windows/settings.vue +++ b/components/app/windows/settings.vue @@ -62,6 +62,24 @@ const submitCustomApiKey = async () => { return; } } + try { + const req = await fetch("/api/user/submitGroqKey", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + value: customApiKey, + }), + }); + + const response = await req.json(); + if (response.error) { + console.error("Error updating user data:", response.error); + } + } catch (error) { + console.error("Failed to submit change:", error); + } }; const checkValidApiKey = () => { @@ -80,11 +98,6 @@ const confirmDelete = async () => { showDeleteDialog.value = false; }; -const deleteAccount = async () => { - const req = await fetch("/api/user/action", { - method: "DELETE", - }); -}; const apiKey = customApiKey.value; try { const sendApi = await fetch("/api/ai/loadCustomGroqApi", { diff --git a/server/api/user/submitGroqKey.ts b/server/api/user/submitGroqKey.ts new file mode 100644 index 0000000..31e36cb --- /dev/null +++ b/server/api/user/submitGroqKey.ts @@ -0,0 +1,36 @@ +import sql from "~/server/components/postgres"; +export default defineEventHandler(async (event) => { + // Check user data. + const userToken = getCookie(event, "token"); + if (!userToken) { + return { + error: "ERR_NOT_ALLOWED", + }; + } + const checkUserToken = await sql` + select * from usertokens + where token=${userToken} + `; + if (checkUserToken.length === 0) { + return { + error: "ERR_NOT_ALLOWED", + }; + } + // Actual function + const body = await readBody(event); + const clearBadDataRegex = /[@-_.+a-zA-Z0-9]{2,}/; + const requestChange = "groq_api_key"; + const apiKeyqq = body.value.match(clearBadDataRegex); + + const sqlC = await sql.unsafe( + ` + UPDATE user_other_data SET ${requestChange} = $1 + WHERE username = $2`, + [apiKeyqq[0], checkUserToken[0].username], + ); + return { + body: body, + data: body.value.match(clearBadDataRegex), + sqlC: sqlC, + }; +});