import { useForm } from '@tanstack/react-form' import { createFileRoute, Link } from '@tanstack/react-router' import { useState } from 'react' import { TextField } from '~/components/form/text-field' import { Button } from '~/components/ui/button' import { Spinner } from '~/components/ui/spinner' import { AuthCard } from '~/features/auth/auth-card' import { FormAlert } from '~/features/auth/form-alert' import { ForgotPasswordSchema } from '~/features/auth/schemas' import { authClient } from '~/server/better-auth/client' export const Route = createFileRoute('/auth/forgot-password')({ component: ForgotPassword, }) function ForgotPassword() { const [formError, setFormError] = useState(null) const [sent, setSent] = useState(false) const form = useForm({ defaultValues: { email: '' }, validators: { onBlur: ForgotPasswordSchema }, onSubmit: async ({ value }) => { setFormError(null) const { error } = await authClient.requestPasswordReset({ email: value.email, redirectTo: '/auth/reset-password', }) if (error) { setFormError(error.message ?? 'Could not send. Try again.') return } setSent(true) }, }) return ( Back to sign in } title="Forgot password" > {sent ? ( If an account exists for this address, a reset email has just been sent. Check your spam folder. ) : (
{ e.preventDefault() form.handleSubmit() }} > {formError ? {formError} : null} {(field) => ( )} [s.canSubmit, s.isSubmitting]}> {([canSubmit, isSubmitting]) => ( )}
)}
) }