Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve validation for OtpForm and LoginForm #1271

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/client/components/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,35 +71,38 @@ export const LoginForm: FC<LoginFormProps> = ({ email, onLogin }) => {
)
}

const hasError = () => errors.email || auth.verifyOtp.isError
const hasError = (): boolean => !!errors.token || auth.verifyOtp.isError

const getErrorMessage = (): string => {
const { otp } = errors
return otp && otp.type === 'required'
return errors && errors.token
? 'Please provide a valid OTP'
: getApiErrorMessage(auth.verifyOtp.error)
: auth.verifyOtp.isError
? getApiErrorMessage(auth.verifyOtp.error)
: ''
}

return (
<form onSubmit={handleSubmit(onSubmit)}>
<VStack spacing="32px" align="stretch">
<FormControl id="email" isInvalid={hasError()}>
<FormLabel color="neutral.900">One time password</FormLabel>
<FormControl id="token" isInvalid={hasError()}>
<FormLabel color="neutral.900" isRequired>
One time password
</FormLabel>
<Text color="neutral.700" mb="24px">
Please enter the OTP sent to {email}.
</Text>
<Input
h="48px"
type="text"
inputMode="numeric"
pattern="\d{6}"
{...register('token', { required: true })}
{...register('token', {
required: true,
minLength: 6,
maxLength: 6,
pattern: /^\d{6}/,
})}
autoComplete="one-time-code"
placeholder="e.g. 111111"
/>
{hasError() && (
<FormErrorMessage>{getErrorMessage()}</FormErrorMessage>
)}
<FormErrorMessage children={getErrorMessage()} />
</FormControl>
<HStack justifyContent="flex-start" spacing={6}>
<Button
Expand Down
16 changes: 9 additions & 7 deletions src/client/components/OtpForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ export const OtpForm: FC<OtpFormProps> = ({ onSuccess }) => {
sendOtp.mutate(formattedEmail)
}

const hasError = () => errors.email || sendOtp.isError
const hasError = (): boolean => !!errors.email || sendOtp.isError

const getErrorMessage = (): string => {
const { email } = errors
return email && email.type === 'required'
? 'Please provide a valid email address'
return errors && errors.email
? 'Please provide a valid .gov.sg email address'
: getApiErrorMessage(sendOtp.error)
}

Expand All @@ -55,12 +54,15 @@ export const OtpForm: FC<OtpFormProps> = ({ onSuccess }) => {
<FormLabel label="neutral.900">Login</FormLabel>
<Text color="neutral.700" mb="24px">
Only available for use by public officers with a{' '}
<strong>gov.sg</strong> email.
<strong>.gov.sg</strong> email.
</Text>
<Input
h="48px"
type="email"
{...register('email', { required: true })}
{...register('email', {
required: true,
pattern:
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+.gov.sg$/,
})}
placeholder="e.g. [email protected]"
/>
{hasError() && (
Expand Down