import { useForm } from '@tanstack/react-form' import { createFileRoute, Link, useNavigate } 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 { AuthDivider, GoogleButton } from '~/features/auth/google-button' import { SignInSchema } from '~/features/auth/schemas' import { authClient } from '~/server/better-auth/client' export const Route = createFileRoute('/auth/sign-in')({ component: SignIn }) function SignIn() { const navigate = useNavigate() const [formError, setFormError] = useState(null) const form = useForm({ defaultValues: { email: '', password: '' }, validators: { onBlur: SignInSchema }, onSubmit: async ({ value }) => { setFormError(null) const { error } = await authClient.signIn.email({ email: value.email, password: value.password, }) if (!error) { await navigate({ to: '/' }) return } if (error.code === 'EMAIL_NOT_VERIFIED') { await navigate({ to: '/auth/verify-email', search: { email: value.email } }) return } setFormError( error.code === 'INVALID_EMAIL_OR_PASSWORD' ? 'Incorrect email or password.' : (error.message ?? 'Could not sign in. Try again.'), ) }, }) return ( No account yet?{' '} Create an account } title="Sign in" >
{ e.preventDefault() form.handleSubmit() }} > {formError ? {formError} : null} {(field) => ( )} {(field) => (
Forgot password?
)}
[s.canSubmit, s.isSubmitting]}> {([canSubmit, isSubmitting]) => ( )}
) }