import Button from '@mui/material/Button' import InputAdornment from '@mui/material/InputAdornment' import Stack from '@mui/material/Stack' import TextField from '@mui/material/TextField' import { useMutation } from '@tanstack/react-query' import { Mail } from 'lucide-react' import { useForm } from 'react-hook-form' import { apiClient } from '@/shared/api/client' import { getApiErrorMessage } from '@/shared/lib/get-api-error-message' import { tokenSet } from '@/shared/model/auth' type AuthResponse = { token: string user: { id: string email: string displayName?: string | null avatar?: string | null avatarStyle?: string | null } } type FormValues = { email: string code: string } type Props = { onSuccess: () => void } export function AuthCodeForm({ onSuccess }: Props) { const { register, watch } = useForm({ defaultValues: { email: '', code: '' }, mode: 'onChange', }) const email = watch('email') const code = watch('code') const requestCodeMutation = useMutation({ mutationFn: async () => { await apiClient.post('auth/request-code', { email }) }, }) const verifyCodeMutation = useMutation({ mutationFn: async () => { const { data } = await apiClient.post('auth/verify-code', { email, code }) tokenSet(data.token) }, onSuccess, }) return ( ), }, }} /> {(requestCodeMutation.error || verifyCodeMutation.error) && ( )} ) }