diff --git a/src/pages/Login.tsx b/src/pages/Login.tsx index 047f4f6b..b79c3418 100644 --- a/src/pages/Login.tsx +++ b/src/pages/Login.tsx @@ -28,7 +28,10 @@ import github2 from '@/assets/github2.svg'; import google from '@/assets/google.svg'; import WindowControls from '@/components/WindowControls'; import { hasStackKeys } from '@/lib'; -import { loginByStackWithAutoCreate } from '@/service/stackAuthApi'; +import { + getLoginErrorMessage as getLoginErrorMessageBase, + loginByStackWithAutoCreate, +} from '@/service/stackAuthApi'; import { useTranslation } from 'react-i18next'; const HAS_STACK_KEYS = hasStackKeys(); @@ -83,41 +86,12 @@ export default function Login() { }; const getLoginErrorMessage = useCallback( - (data: any) => { - if (!data || typeof data !== 'object' || typeof data.code !== 'number') { - return ''; - } - - if (data.code === 0) { - return ''; - } - - if (data.code === 10) { - return ( - data.text || - t('layout.login-failed-please-check-your-email-and-password') - ); - } - - if ( - data.code === 1 && - Array.isArray(data.error) && - data.error.length > 0 - ) { - const firstError = data.error[0]; - if (typeof firstError === 'string') { - return firstError; - } - if (typeof firstError?.msg === 'string') { - return firstError.msg; - } - if (typeof firstError?.message === 'string') { - return firstError.message; - } - } - - return data.text || t('layout.login-failed-please-try-again'); - }, + (data: any) => + getLoginErrorMessageBase( + data, + t('layout.login-failed-please-check-your-email-and-password'), + t('layout.login-failed-please-try-again') + ), [t] ); @@ -441,35 +415,39 @@ export default function Login() { {t('layout.sign-up')} -
diff --git a/src/pages/SignUp.tsx b/src/pages/SignUp.tsx index c73598ff..149b0d94 100644 --- a/src/pages/SignUp.tsx +++ b/src/pages/SignUp.tsx @@ -27,7 +27,10 @@ import eye from '@/assets/eye.svg'; import github2 from '@/assets/github2.svg'; import google from '@/assets/google.svg'; import { hasStackKeys } from '@/lib'; -import { loginByStackToken } from '@/service/stackAuthApi'; +import { + getLoginErrorMessage as getLoginErrorMessageBase, + loginByStackToken, +} from '@/service/stackAuthApi'; import { useTranslation } from 'react-i18next'; const HAS_STACK_KEYS = hasStackKeys(); @@ -88,41 +91,12 @@ export default function SignUp() { }; const getLoginErrorMessage = useCallback( - (data: any) => { - if (!data || typeof data !== 'object' || typeof data.code !== 'number') { - return ''; - } - - if (data.code === 0) { - return ''; - } - - if (data.code === 10) { - return ( - data.text || - t('layout.login-failed-please-check-your-email-and-password') - ); - } - - if ( - data.code === 1 && - Array.isArray(data.error) && - data.error.length > 0 - ) { - const firstError = data.error[0]; - if (typeof firstError === 'string') { - return firstError; - } - if (typeof firstError?.msg === 'string') { - return firstError.msg; - } - if (typeof firstError?.message === 'string') { - return firstError.message; - } - } - - return data.text || t('layout.login-failed-please-try-again'); - }, + (data: any) => + getLoginErrorMessageBase( + data, + t('layout.login-failed-please-check-your-email-and-password'), + t('layout.login-failed-please-try-again') + ), [t] ); @@ -369,35 +343,39 @@ export default function SignUp() { {t('layout.login')}
diff --git a/src/service/stackAuthApi.ts b/src/service/stackAuthApi.ts
index 0f2ba259..0ff4c9de 100644
--- a/src/service/stackAuthApi.ts
+++ b/src/service/stackAuthApi.ts
@@ -56,6 +56,39 @@ export async function loginByStackToken(params: {
* Attempts a passwordless SSO login first, and auto-creates the user if not found.
* This matches the UX request: “check existing profile; if missing, create like signup”.
*/
+export function getLoginErrorMessage(
+ data: any,
+ fallbackCheckEmail: string,
+ fallbackGeneric: string
+): string {
+ if (!data || typeof data !== 'object' || typeof data.code !== 'number') {
+ return '';
+ }
+
+ if (data.code === 0) {
+ return '';
+ }
+
+ if (data.code === 10) {
+ return data.text || fallbackCheckEmail;
+ }
+
+ if (data.code === 1 && Array.isArray(data.error) && data.error.length > 0) {
+ const firstError = data.error[0];
+ if (typeof firstError === 'string') {
+ return firstError;
+ }
+ if (typeof firstError?.msg === 'string') {
+ return firstError.msg;
+ }
+ if (typeof firstError?.message === 'string') {
+ return firstError.message;
+ }
+ }
+
+ return data.text || fallbackGeneric;
+}
+
export async function loginByStackWithAutoCreate(
token: string
): Promise