From 1e3d27db2965bc28990c64c8d72c9bb3539f3b8b Mon Sep 17 00:00:00 2001 From: niko <57009359+hauntii@users.noreply.github.com> Date: Fri, 25 Apr 2025 21:32:17 -0400 Subject: [PATCH] fix: clean up some api things --- src/pages/account/login/index.vue | 33 ++++++++++++++---------- src/server/api/account/login/index.ts | 4 +-- src/server/api/account/register/index.ts | 5 ++-- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/pages/account/login/index.vue b/src/pages/account/login/index.vue index 66da032..5fd037b 100644 --- a/src/pages/account/login/index.vue +++ b/src/pages/account/login/index.vue @@ -9,22 +9,27 @@ const loginForm = reactive({ username: '', password: '' }); const errorMessage = ref(); async function loginSubmission() { - await $fetch('/api/account/login', { - method: 'POST', - body: loginForm - }).catch((error: FetchError) => { - errorMessage.value = error.statusText; - setTimeout(() => { // TODO: this is not the best way to clear this out, but this is temporary! replace all toasts with input alerts in the future + try { + await $fetch('/api/account/login', { + method: 'POST', + body: loginForm + }); + + if (typeof redirect.value === 'string') { + await navigateTo(redirect.value); + } else { + await navigateTo('/account'); + } + } catch (error: unknown) { + if (error instanceof FetchError) { + errorMessage.value = error.statusText; + } else { + errorMessage.value = `Error during registration: ${error}`; // TODO: localize + } + + setTimeout(() => { // TODO: replace this toast errorMessage.value = null; }, 5000); - - return; - }); - - if (typeof redirect.value === 'string') { - await navigateTo(redirect.value); - } else { - await navigateTo('/account'); } } diff --git a/src/server/api/account/login/index.ts b/src/server/api/account/login/index.ts index 61c2407..25749ca 100644 --- a/src/server/api/account/login/index.ts +++ b/src/server/api/account/login/index.ts @@ -1,11 +1,11 @@ import { FetchError } from 'ofetch'; -type LoginCCResponse = { +interface LoginCCResponse { refresh_token: string; access_token: string; token_type: string; expires_in: number; -}; +} export default defineEventHandler(async (event) => { const body = await readBody(event); diff --git a/src/server/api/account/register/index.ts b/src/server/api/account/register/index.ts index 5536ca5..f27c9bd 100644 --- a/src/server/api/account/register/index.ts +++ b/src/server/api/account/register/index.ts @@ -1,12 +1,11 @@ import { FetchError } from 'ofetch'; -// TODO: Drop to interface from type, type is unnecessary for this -type RegisterCCResponse = { // TODO: this is the same as the login response. figure out where we should put types and merge into AuthCCReponse! +interface RegisterCCResponse { // TODO: this is the same as the login response. figure out where we should put types and merge into AuthCCReponse! refresh_token: string; access_token: string; token_type: string; expires_in: number; -}; +} export default defineEventHandler(async (event) => { const body = await readBody(event);