Skip to content

Commit 0b76f1e

Browse files
agusmdevclaude
andcommitted
refactor(frontend): typed auth payload helpers, remove redundant id guard
- schemas.ts: add loginFormToPayload() and registerFormToPayload() — moves field-name translation (password → raw_password) from call sites into the schema boundary where it belongs - login.tsx/register.tsx: use payload helpers; callers no longer know backend field names - AuthContext.login: remove !response.id falsiness guard — id is typed as required string; runtime guard contradicts the type contract Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7d6aff6 commit 0b76f1e

4 files changed

Lines changed: 14 additions & 7 deletions

File tree

template/frontend/src/contexts/AuthContext.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ export function AuthProvider({ children, queryClient }: AuthProviderProps) {
4444
}, [queryClient, router])
4545

4646
const login = useCallback((response: AuthSessionResponse) => {
47-
if (!response.id) {
48-
throw new Error('No session token returned from server')
49-
}
5047
setAuthToken(response.id)
5148
setTokenState(response.id)
5249
}, [])

template/frontend/src/lib/schemas.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,13 @@ export type ItemFormData = z.infer<typeof itemSchema>
3030
export function itemFormToPayload(data: ItemFormData) {
3131
return { name: data.name, description: data.description || undefined }
3232
}
33+
34+
/** Maps login form values to the API payload (field names match the login endpoint). */
35+
export function loginFormToPayload(data: LoginFormData) {
36+
return { email: data.email, password: data.password }
37+
}
38+
39+
/** Maps register form values to the API payload (renames password → raw_password per backend contract). */
40+
export function registerFormToPayload(data: RegisterFormData) {
41+
return { email: data.email, raw_password: data.password }
42+
}

template/frontend/src/routes/login.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { AuthFormShell } from '@/components/AuthFormShell'
77
import { EmailField, PasswordField } from '@/components/AuthFormFields'
88
import { API } from '@/lib/api-endpoints'
99
import { useAuthSubmit } from '@/hooks/useAuthSubmit'
10-
import { loginSchema, type LoginFormData } from '@/lib/schemas'
10+
import { loginSchema, loginFormToPayload, type LoginFormData } from '@/lib/schemas'
1111

1212
export const Route = createFileRoute('/login')({
1313
validateSearch: (search: Record<string, unknown>) => ({
@@ -26,7 +26,7 @@ function Login() {
2626
})
2727

2828
const onSubmit = (data: LoginFormData) =>
29-
submit({ email: data.email, password: data.password })
29+
submit(loginFormToPayload(data))
3030

3131
return (
3232
<AuthFormShell

template/frontend/src/routes/register.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { AuthFormShell } from '@/components/AuthFormShell'
77
import { EmailField, PasswordField } from '@/components/AuthFormFields'
88
import { API } from '@/lib/api-endpoints'
99
import { useAuthSubmit } from '@/hooks/useAuthSubmit'
10-
import { registerSchema, type RegisterFormData } from '@/lib/schemas'
10+
import { registerSchema, registerFormToPayload, type RegisterFormData } from '@/lib/schemas'
1111

1212
export const Route = createFileRoute('/register')({
1313
component: Register,
@@ -26,7 +26,7 @@ function Register() {
2626
})
2727

2828
const onSubmit = (data: RegisterFormData) =>
29-
submit({ email: data.email, raw_password: data.password })
29+
submit(registerFormToPayload(data))
3030

3131
return (
3232
<AuthFormShell

0 commit comments

Comments
 (0)