diff --git a/frontend/website/src/stores/auth.ts b/frontend/website/src/stores/auth.ts index 6460027..d720f18 100644 --- a/frontend/website/src/stores/auth.ts +++ b/frontend/website/src/stores/auth.ts @@ -13,27 +13,12 @@ const meResponseSchema = z.object({ user: authUserSchema, }) -const invalidCredentialsResponseSchema = z.object({ - error: z.literal('invalid_credentials'), -}) - -const loginValidationResponseSchema = z.object({ - errors: z.object({ - email: z.array(z.string()).optional(), - password: z.array(z.string()).optional(), - }), +const loginErrorResponseSchema = z.object({ + error: z.string(), }) export type AuthUser = z.infer export type LoginFieldErrors = Partial> -export type LoginResult = - | { - success: true - } - | { - success: false - fieldErrors: LoginFieldErrors - } export const useAuthStore = defineStore('auth', () => { const user = ref(null) @@ -77,7 +62,7 @@ export const useAuthStore = defineStore('auth', () => { } } - async function login(email: string, password: string): Promise { + async function login(email: string, password: string): Promise { loading.value = true error.value = null @@ -96,43 +81,21 @@ export const useAuthStore = defineStore('auth', () => { if (response.status === 200) { user.value = meResponseSchema.parse(responseBody).user - return { success: true } + return true } user.value = null - if ( - response.status === 401 && - invalidCredentialsResponseSchema.safeParse(responseBody).success - ) { - error.value = 'Email or password is incorrect.' + const errorResponse = loginErrorResponseSchema.safeParse(responseBody) + error.value = errorResponse.success + ? errorResponse.data.error + : 'Unable to log in. Please try again.' - return { success: false, fieldErrors: {} } - } - - if (response.status === 422) { - const validationResponse = loginValidationResponseSchema.safeParse(responseBody) - if (validationResponse.success) { - return { - success: false, - fieldErrors: firstLoginFieldErrors(validationResponse.data.errors), - } - } - } - - if (response.status === 429) { - error.value = 'Too many login attempts. Try again in a minute.' - - return { success: false, fieldErrors: {} } - } - - error.value = 'Unable to log in. Please try again.' - - return { success: false, fieldErrors: {} } + return false } catch { user.value = null error.value = 'Unable to log in. Please try again.' - return { success: false, fieldErrors: {} } + return false } finally { loading.value = false } @@ -147,20 +110,3 @@ export const useAuthStore = defineStore('auth', () => { login, } }) - -function firstLoginFieldErrors( - errors: Partial>, -): LoginFieldErrors { - const fieldErrors: LoginFieldErrors = {} - const emailError = errors.email?.[0] - const passwordError = errors.password?.[0] - - if (emailError !== undefined) { - fieldErrors.email = emailError - } - if (passwordError !== undefined) { - fieldErrors.password = passwordError - } - - return fieldErrors -} diff --git a/frontend/website/src/views/LoginView.vue b/frontend/website/src/views/LoginView.vue index 2045c38..3088dec 100644 --- a/frontend/website/src/views/LoginView.vue +++ b/frontend/website/src/views/LoginView.vue @@ -25,10 +25,8 @@ async function submitLogin(): Promise { return } - const result = await authStore.login(normalizedEmail, password.value) - if (!result.success) { - fieldErrors.value = result.fieldErrors - + const loggedIn = await authStore.login(normalizedEmail, password.value) + if (!loggedIn) { return }