Skip to content

Commit

Permalink
fix: show form error if login credentials are incorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
torrance-stripe committed Oct 23, 2024
1 parent e4f5e97 commit 7b7c237
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions app/(auth)/login/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,26 @@ export default function LoginForm() {

const onSubmit = async (values: z.infer<typeof formSchema>) => {
try {
await signIn('login', {
const result = await signIn('login', {
email: values.email,
password: values.password,
redirect: false,
});

router.push('/home');
if (result?.error) {
form.setError('root', {
type: 'manual',
message: 'Invalid email or password. Please try again.',
});
} else if (result?.ok) {
router.push('/home');
}
} catch (error: any) {
console.error('An error occurred when signing in', error);
form.setError('root', {
type: 'manual',
message: 'An unexpected error occurred. Please try again.',
});
}
};

Expand Down Expand Up @@ -108,6 +119,11 @@ export default function LoginForm() {
</>
)}
</Button>
{form.formState.errors.root && (
<p className="text-sm text-red-500">
{form.formState.errors.root.message}
</p>
)}
</form>
</Form>
);
Expand Down

0 comments on commit 7b7c237

Please sign in to comment.