Skip to content

Commit

Permalink
fix(core): handle invalid credentials error message in Login (#1944)
Browse files Browse the repository at this point in the history
* fix(core): handle invalid credentials error message in Login

* fix: remove nested conditionals
  • Loading branch information
jorgemoya authored Jan 30, 2025
1 parent a1177c2 commit c5ce9dc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-kiwis-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": patch
---

Properly handle the auth error when login is invalid.
12 changes: 9 additions & 3 deletions core/app/[locale]/(default)/(auth)/login/_actions/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { BigCommerceGQLError } from '@bigcommerce/catalyst-client';
import { SubmissionResult } from '@conform-to/react';
import { parseWithZod } from '@conform-to/zod';
import { AuthError } from 'next-auth';
import { getLocale, getTranslations } from 'next-intl/server';

import { schema } from '@/vibes/soul/sections/sign-in-section/schema';
Expand Down Expand Up @@ -42,11 +43,16 @@ export const login = async (_lastResult: SubmissionResult | null, formData: Form
});
}

if (error instanceof Error) {
return submission.reply({ formErrors: [error.message] });
if (
error instanceof AuthError &&
error.name === 'CallbackRouteError' &&
error.cause &&
error.cause.err?.message.includes('Invalid credentials')
) {
return submission.reply({ formErrors: [t('Form.invalidCredentials')] });
}

return submission.reply({ formErrors: [t('Form.error')] });
return submission.reply({ formErrors: [t('Form.somethingWentWrong')] });
}

return redirect({ href: '/account/orders', locale });
Expand Down
5 changes: 3 additions & 2 deletions core/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,16 @@
"title": "Login",
"heading": "Log In",
"Form": {
"error": "Your email address or password is incorrect. Try signing in again or reset your password",
"invalidCredentials": "Your email address or password is incorrect. Try signing in again or reset your password",
"successful": "You have successfully logged in.",
"emailLabel": "Email",
"enterEmailMessage": "Enter your email",
"passwordLabel": "Password",
"enterPasswordMessage": "Enter your password",
"submitting": "Submitting...",
"logIn": "Log in",
"forgotPassword": "Forgot your password?"
"forgotPassword": "Forgot your password?",
"somethingWentWrong": "Something went wrong. Please try again later."
},
"CreateAccount": {
"heading": "New customer?",
Expand Down

0 comments on commit c5ce9dc

Please sign in to comment.