/** * Supabase says what went wrong in English, to a developer. * * The person reading it is neither. A sign-in screen in Arabic that answers a * mistyped password with «Invalid login credentials» has told them nothing - * and that particular sentence is worse than nothing, because Supabase also * uses it for an address that was never confirmed. Someone retyping a correct * password could read it all day. * * Matched on `code` first: supabase-js carries a stable one, and the English * wording behind it changes between releases. The message is only a fallback * for the older shapes that carry no code. */ import type { AuthError } from "@supabase/supabase-js"; const BY_CODE: Record = { invalid_credentials: "البريد أو كلمة المرور غير صحيحة.", email_not_confirmed: "هذا البريد لم يُفعَّل بعد. راجع المسؤول.", user_already_exists: "هذا البريد مسجّل بالفعل — سجّل الدخول به.", email_exists: "هذا البريد مسجّل بالفعل — سجّل الدخول به.", weak_password: "كلمة المرور ضعيفة. اجعلها ٨ أحرف على الأقل.", same_password: "اختر كلمة مرور مختلفة عن السابقة.", over_email_send_rate_limit: "حاولت مرات كثيرة. انتظر دقيقة ثم أعد المحاولة.", over_request_rate_limit: "حاولت مرات كثيرة. انتظر دقيقة ثم أعد المحاولة.", otp_expired: "انتهت صلاحية الرابط. اطلب رابطًا جديدًا.", validation_failed: "تحقّق من البيانات المكتوبة.", user_not_found: "لا يوجد حساب بهذا البريد.", session_not_found: "انتهت الجلسة. سجّل الدخول من جديد.", signup_disabled: "التسجيل موقوف حاليًا.", email_address_invalid: "صيغة البريد غير صحيحة.", }; /** For the releases that send a sentence and no code. */ const BY_TEXT: [RegExp, string][] = [ [/invalid login credentials/i, BY_CODE.invalid_credentials], [/email not confirmed/i, BY_CODE.email_not_confirmed], [/already registered|already been registered/i, BY_CODE.user_already_exists], [/password should be at least/i, BY_CODE.weak_password], [/should be different from the old/i, BY_CODE.same_password], [/rate limit|only request this after/i, BY_CODE.over_email_send_rate_limit], [/expired|invalid.*token/i, BY_CODE.otp_expired], [/unable to validate email|invalid format/i, BY_CODE.email_address_invalid], [/failed to fetch|network/i, "تعذّر الاتصال. تحقّق من الشبكة وأعد المحاولة."], ]; export function inArabic(error: AuthError | Error | null): string { if (!error) return ""; const code = (error as AuthError).code; if (code && BY_CODE[code]) return BY_CODE[code]; const text = error.message ?? ""; for (const [pattern, arabic] of BY_TEXT) { if (pattern.test(text)) return arabic; } // Nothing recognised. The English is still better than silence, and seeing // one here is the signal that it belongs in a table above. return text || "حدث خطأ غير متوقع. أعد المحاولة."; }