diff --git a/.changeset/flat-kiwis-design.md b/.changeset/flat-kiwis-design.md new file mode 100644 index 0000000000..56b4001b48 --- /dev/null +++ b/.changeset/flat-kiwis-design.md @@ -0,0 +1,5 @@ +--- +"@bigcommerce/catalyst-core": patch +--- + +Properly handle the auth error when login is invalid. diff --git a/core/app/[locale]/(default)/(auth)/login/_actions/login.ts b/core/app/[locale]/(default)/(auth)/login/_actions/login.ts index 644ba7c800..c1391956b6 100644 --- a/core/app/[locale]/(default)/(auth)/login/_actions/login.ts +++ b/core/app/[locale]/(default)/(auth)/login/_actions/login.ts @@ -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'; @@ -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 }); diff --git a/core/messages/en.json b/core/messages/en.json index 1eb2390180..9eb8581820 100644 --- a/core/messages/en.json +++ b/core/messages/en.json @@ -66,7 +66,7 @@ "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", @@ -74,7 +74,8 @@ "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?",