diff --git a/app/components/auth/form/login-form.tsx b/app/components/auth/form/login-form.tsx new file mode 100644 index 0000000..3c449b3 --- /dev/null +++ b/app/components/auth/form/login-form.tsx @@ -0,0 +1,106 @@ +import { useState } from 'react'; +import { Controller, useForm } from 'react-hook-form'; +import { zodResolver } from '@hookform/resolvers/zod'; +import { useNavigation, useSubmit } from 'react-router'; +import { Mail, Shield } from 'lucide-react'; +import { toast } from 'sonner'; +import { AccessNote } from '~/components/auth/form/access-note'; +import { PasswordInput } from '~/components/auth/form/password-input'; +import { SignInButton } from '~/components/auth/form/sign-in-button'; +import { Alert } from '~/components/ui/alert'; +import { Button } from '~/components/ui/button'; +import { Checkbox } from '~/components/ui/checkbox'; +import { Field } from '~/components/ui/field'; +import { IconInput } from '~/components/ui/icon-input'; +import { Text } from '~/components/ui/text'; +import { ROUTES } from '~/data/routes'; +import { loginSchema, type LoginValues } from '~/schemas/auth/login'; +import type { LoginFailure } from '~/types/auth'; + +type TextField = 'email' | 'password'; + +export function LoginForm({ failure }: { failure?: LoginFailure }) { + const submit = useSubmit(); + const navigation = useNavigation(); + const busy = navigation.state !== 'idle'; + // Loading a page other than /login means the sign-in worked and the admin is on its way + const signedIn = navigation.state === 'loading' && navigation.location.pathname !== ROUTES.login; + // The failure the user has typed since; its banner and red borders are hidden + const [dismissed, setDismissed] = useState(); + const shown = failure === dismissed ? undefined : failure; + const { + register, + control, + clearErrors, + handleSubmit, + formState: { errors }, + } = useForm({ resolver: zodResolver(loginSchema), reValidateMode: 'onSubmit', defaultValues: { email: '', password: '', remember: true } }); + + // Typing clears that field's error and the banner + const textField = (name: TextField) => ({ + ...register(name, { + onChange: () => { + clearErrors(name); + setDismissed(failure); + }, + }), + 'aria-invalid': !!errors[name] || !!shown?.invalidCredentials, + }); + + return ( +
submit(values, { method: 'post', encType: 'application/json' }))} + noValidate + className='flex w-full max-w-[372px] flex-col gap-7.5' + > +
+ Admin portal + Welcome back + Sign in to manage client projects, 3D assets and QR codes. +
+ + {shown && {shown.error}} + +
+ + + + + toast.info('Ask your workspace admin to reset your password.')}> + Forgot password? + + } + > + + + + ( +
+ onChange(checked === true)} /> + + Keep me signed in for 30 days + +
+ )} + /> +
+ +
+ + + + Access is restricted to authorised team members. + + +
+
+ ); +}