Skip to content

Commit

Permalink
Show form error if login credentials are incorrect (#190)
Browse files Browse the repository at this point in the history
* fix: show form error if login credentials are incorrect

* fix: have sign up emit errors as well
  • Loading branch information
torrance-stripe authored Oct 23, 2024
1 parent e4f5e97 commit 6bed31d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 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
17 changes: 15 additions & 2 deletions app/(auth)/signup/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,21 @@ export default function SignupForm() {

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

router.push('/business');
if (result?.error) {
// Just assume the error is because the email is already in use
form.setError('root', {
type: 'manual',
message: 'The email is already in use.',
});
} else if (result?.ok) {
router.push('/business');
}
} catch (error: any) {
console.error('An error occurred when signing in', error);
}
Expand Down Expand Up @@ -106,6 +114,11 @@ export default function SignupForm() {
</div>
)}
</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 6bed31d

Please sign in to comment.