forked from rajun/ar-test
107 lines
4.8 KiB
TypeScript
107 lines
4.8 KiB
TypeScript
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<LoginFailure>();
|
|
const shown = failure === dismissed ? undefined : failure;
|
|
const {
|
|
register,
|
|
control,
|
|
clearErrors,
|
|
handleSubmit,
|
|
formState: { errors },
|
|
} = useForm<LoginValues>({ 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 (
|
|
<form
|
|
onSubmit={handleSubmit((values) => submit(values, { method: 'post', encType: 'application/json' }))}
|
|
noValidate
|
|
className='flex w-full max-w-[372px] flex-col gap-7.5'
|
|
>
|
|
<div className='flex flex-col gap-2.5'>
|
|
<Text as='span' variant='kicker'>Admin portal</Text>
|
|
<Text as='h1' variant='headline'>Welcome back</Text>
|
|
<Text variant='body-lg' tone='subtle'>Sign in to manage client projects, 3D assets and QR codes.</Text>
|
|
</div>
|
|
|
|
{shown && <Alert>{shown.error}</Alert>}
|
|
|
|
<fieldset disabled={busy} className='flex min-w-0 flex-col gap-4.5'>
|
|
<Field label='Email' htmlFor='email' error={errors.email?.message}>
|
|
<IconInput id='email' type='email' icon={Mail} size='lg' autoComplete='email' autoFocus placeholder='you@company.com' {...textField('email')} />
|
|
</Field>
|
|
|
|
<Field
|
|
label='Password'
|
|
htmlFor='password'
|
|
error={errors.password?.message}
|
|
action={
|
|
<Button type='button' variant='link-muted' size='inline' onClick={() => toast.info('Ask your workspace admin to reset your password.')}>
|
|
Forgot password?
|
|
</Button>
|
|
}
|
|
>
|
|
<PasswordInput id='password' size='lg' autoComplete='current-password' placeholder='Your password' {...textField('password')} />
|
|
</Field>
|
|
|
|
<Controller
|
|
control={control}
|
|
name='remember'
|
|
render={({ field: { value, onChange } }) => (
|
|
<div className='flex items-center gap-2.5 self-start'>
|
|
<Checkbox id='remember' checked={value} onCheckedChange={(checked) => onChange(checked === true)} />
|
|
<Text as='label' htmlFor='remember' className='cursor-pointer'>
|
|
Keep me signed in for 30 days
|
|
</Text>
|
|
</div>
|
|
)}
|
|
/>
|
|
</fieldset>
|
|
|
|
<div className='flex flex-col gap-4'>
|
|
<SignInButton state={signedIn ? 'done' : busy ? 'busy' : 'idle'} />
|
|
<Text variant='caption' className='flex items-center justify-center gap-2'>
|
|
<Shield size={13} />
|
|
Access is restricted to authorised team members.
|
|
</Text>
|
|
<AccessNote className='text-center sm:hidden' />
|
|
</div>
|
|
</form>
|
|
);
|
|
}
|