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,
})
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<typeof authUserSchema>
export type LoginFieldErrors = Partial<Record<'email' | 'password', string>>
export type LoginResult =
| {
success: true
}
| {
success: false
fieldErrors: LoginFieldErrors
}
export const useAuthStore = defineStore('auth', () => {
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
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<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
}
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
}