diff --git a/components/app/windows/settings.vue b/components/app/windows/settings.vue index ae6ec37..2db04b5 100644 --- a/components/app/windows/settings.vue +++ b/components/app/windows/settings.vue @@ -100,23 +100,24 @@ const checkValidApiKey = () => { const showDeleteDialog = ref(false); const showLogoutDialog = ref(false); const confirmDelete = async () => { + showDeleteDialog.value = false; await deleteAccount(); await validateUserInfo(); - showDeleteDialog.value = false; }; const deleteAccount = async () => { const req = await fetch("/api/user/sendUserChanges", { method: "DELETE", }); - const res = await res.json(); + const res = await req.json(); console.log(res); }; const submitChangeAction = async (action: string) => { + //const allowedColumns = ["firstname", "email"]; const actions = [ - { name: "NAME", sendValue: enterFirstName.value }, - { name: "USER_EMAIL", sendValue: enteruseremail.value }, + { name: "NAME", SQLSystem: "firstname", sendValue: enterFirstName.value }, + { name: "USER_EMAIL", SQLSystem: "email", sendValue: enteruseremail.value }, ]; const actionMatch = actions.find((a) => a.name === action); @@ -131,7 +132,7 @@ const submitChangeAction = async (action: string) => { "Content-Type": "application/json", }, body: JSON.stringify({ - action: actionMatch.name, + action: actionMatch.SQLSystem, value: actionMatch.sendValue, jsonValue: "", }), diff --git a/server/api/user/login.ts b/server/api/user/login.ts index 0bd32f1..67dfd42 100644 --- a/server/api/user/login.ts +++ b/server/api/user/login.ts @@ -81,6 +81,9 @@ export default defineEventHandler(async (event) => { VALUES (${fetchUserInfoAgain[0].username}, ${newToken}) `; + const getUserFirstName = await sql` + select * from user_other_data`; + setCookie(event, "token", newToken); return { user: fetchUserInfoAgain, diff --git a/server/api/user/sendUserChanges.post.ts b/server/api/user/sendUserChanges.post.ts index 1dc17a4..6c18f9c 100644 --- a/server/api/user/sendUserChanges.post.ts +++ b/server/api/user/sendUserChanges.post.ts @@ -12,23 +12,26 @@ export default defineEventHandler(async (event) => { const body = await readBody(event); if (body.jsonValue.length === 0) { const clearBadDataRegex = /[@-_.+a-zA-Z0-9]{2,}/; - let allowed = true; - if (body.value.match()) { - allowed = false; - } // Use Static values for now. - const requestChange = "groq_api_key"; + const requestChange = body.name; const apiKeyqq = body.value.match(clearBadDataRegex); - const allowedColumns = ["groq_api_key", "another_column_name"]; + const allowedColumns = ["firstname", "email"]; if (!allowedColumns.includes(requestChange)) { - throw new Error("Invalid column name provided"); + return { + error: "ERR_NOT_ALLOWED", + }; + } else if (requestChange === "name") { + const sqlC = await sql` + UPDATE users SET firstname = ${apiKeyqq[0]} + WHERE username = ${token.user}`; + return { + sqlC: sqlC, + success: true, + }; } - const sqlC = await sql.unsafe( - ` - UPDATE user_other_data SET ${requestChange} = $1 - WHERE username = $2`, + `UPDATE user_other_data SET ${requestChange} = $1 WHERE username = $2`, [apiKeyqq[0], token.user], ); return {