align frontend login contract

This commit is contained in:
Yisroel Baum 2026-08-02 21:02:25 +03:00
parent d52a8bf3b5
commit 942ee4e6e7
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
2 changed files with 12 additions and 68 deletions

View file

@ -13,27 +13,12 @@ const meResponseSchema = z.object({
user: authUserSchema, user: authUserSchema,
}) })
const invalidCredentialsResponseSchema = z.object({ const loginErrorResponseSchema = z.object({
error: z.literal('invalid_credentials'), error: z.string(),
})
const loginValidationResponseSchema = z.object({
errors: z.object({
email: z.array(z.string()).optional(),
password: z.array(z.string()).optional(),
}),
}) })
export type AuthUser = z.infer<typeof authUserSchema> export type AuthUser = z.infer<typeof authUserSchema>
export type LoginFieldErrors = Partial<Record<'email' | 'password', string>> export type LoginFieldErrors = Partial<Record<'email' | 'password', string>>
export type LoginResult =
| {
success: true
}
| {
success: false
fieldErrors: LoginFieldErrors
}
export const useAuthStore = defineStore('auth', () => { export const useAuthStore = defineStore('auth', () => {
const user = ref<AuthUser | null>(null) const user = ref<AuthUser | null>(null)
@ -77,7 +62,7 @@ export const useAuthStore = defineStore('auth', () => {
} }
} }
async function login(email: string, password: string): Promise<LoginResult> { async function login(email: string, password: string): Promise<boolean> {
loading.value = true loading.value = true
error.value = null error.value = null
@ -96,43 +81,21 @@ export const useAuthStore = defineStore('auth', () => {
if (response.status === 200) { if (response.status === 200) {
user.value = meResponseSchema.parse(responseBody).user user.value = meResponseSchema.parse(responseBody).user
return { success: true } return true
} }
user.value = null user.value = null
if ( const errorResponse = loginErrorResponseSchema.safeParse(responseBody)
response.status === 401 && error.value = errorResponse.success
invalidCredentialsResponseSchema.safeParse(responseBody).success ? errorResponse.data.error
) { : 'Unable to log in. Please try again.'
error.value = 'Email or password is incorrect.'
return { success: false, fieldErrors: {} } return false
}
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: {} }
} catch { } catch {
user.value = null user.value = null
error.value = 'Unable to log in. Please try again.' error.value = 'Unable to log in. Please try again.'
return { success: false, fieldErrors: {} } return false
} finally { } finally {
loading.value = false loading.value = false
} }
@ -147,20 +110,3 @@ export const useAuthStore = defineStore('auth', () => {
login, login,
} }
}) })
function firstLoginFieldErrors(
errors: Partial<Record<'email' | 'password', string[]>>,
): 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
}

View file

@ -25,10 +25,8 @@ async function submitLogin(): Promise<void> {
return return
} }
const result = await authStore.login(normalizedEmail, password.value) const loggedIn = await authStore.login(normalizedEmail, password.value)
if (!result.success) { if (!loggedIn) {
fieldErrors.value = result.fieldErrors
return return
} }