diff --git a/src/locales/en.json b/src/locales/en.json index 765f609..96b5497 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -161,6 +161,8 @@ "or": "or", "registerHint": "To register with email, first log in via Telegram, then link your email in settings.", "telegramRequired": "Telegram authorization required", + "telegramRetryFailed": "Authorization failed. Close the app and try again.", + "telegramReopenHint": "If the problem persists, close and reopen the app", "telegramNotConfigured": "Telegram bot is not configured", "authenticating": "Authenticating...", "orOpenInApp": "Or open the bot in the app", diff --git a/src/locales/fa.json b/src/locales/fa.json index 061db03..fcce861 100644 --- a/src/locales/fa.json +++ b/src/locales/fa.json @@ -153,6 +153,8 @@ "or": "یا", "registerHint": "برای ثبت نام با ایمیل، ابتدا با تلگرام وارد شوید، سپس ایمیل خود را در تنظیمات متصل کنید.", "telegramRequired": "نیاز به تایید تلگرام", + "telegramRetryFailed": "تایید هویت ناموفق بود. برنامه را ببندید و دوباره باز کنید.", + "telegramReopenHint": "اگر مشکل ادامه دارد، برنامه را ببندید و دوباره باز کنید", "telegramNotConfigured": "ربات تلگرام پیکربندی نشده", "authenticating": "در حال تایید هویت...", "orOpenInApp": "یا ربات را در برنامه باز کنید", diff --git a/src/locales/ru.json b/src/locales/ru.json index 1462dad..7585644 100644 --- a/src/locales/ru.json +++ b/src/locales/ru.json @@ -164,6 +164,8 @@ "or": "или", "registerHint": "Для регистрации по email сначала авторизуйтесь через Telegram, затем привяжите email в настройках.", "telegramRequired": "Требуется авторизация через Telegram", + "telegramRetryFailed": "Авторизация не удалась. Закройте приложение и откройте заново.", + "telegramReopenHint": "Если проблема повторяется — закройте и откройте приложение заново", "telegramNotConfigured": "Telegram бот не настроен", "authenticating": "Авторизация...", "orOpenInApp": "Или откройте бота в приложении", diff --git a/src/locales/zh.json b/src/locales/zh.json index 73588d9..7520d66 100644 --- a/src/locales/zh.json +++ b/src/locales/zh.json @@ -153,6 +153,8 @@ "or": "或", "registerHint": "要通过邮箱注册,请先通过Telegram登录,然后在设置中绑定邮箱。", "telegramRequired": "需要Telegram授权", + "telegramRetryFailed": "授权失败。请关闭应用后重新打开。", + "telegramReopenHint": "如果问题持续存在,请关闭并重新打开应用", "telegramNotConfigured": "Telegram机器人未配置", "authenticating": "正在验证...", "orOpenInApp": "或在应用中打开机器人", diff --git a/src/pages/Login.tsx b/src/pages/Login.tsx index 5e6bfe3..3a3669a 100644 --- a/src/pages/Login.tsx +++ b/src/pages/Login.tsx @@ -110,31 +110,62 @@ export default function Login() { } }, [isAuthenticated, navigate, getReturnUrl]); - // Try Telegram WebApp authentication on mount + // Try Telegram WebApp authentication on mount (with auto-retry on 401) useEffect(() => { const tryTelegramAuth = async () => { const initData = getTelegramInitData(); - if (isInTelegramWebApp() && initData) { - setIsTelegramWebApp(true); - // Note: ready() and expand() are already called by SDK init in main.tsx - setIsLoading(true); + if (!isInTelegramWebApp() || !initData) return; + + setIsTelegramWebApp(true); + setIsLoading(true); + + const MAX_RETRIES = 1; + for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) { try { await loginWithTelegram(initData); navigate(getReturnUrl(), { replace: true }); + return; } catch (err) { - // Log only status code to avoid leaking sensitive data const status = (err as { response?: { status?: number } })?.response?.status; - console.warn('Telegram auth failed with status:', status); + console.warn(`Telegram auth attempt ${attempt + 1} failed with status:`, status); + + if (status === 401 && attempt < MAX_RETRIES) { + await new Promise((r) => setTimeout(r, 1500)); + continue; + } + setError(t('auth.telegramRequired')); - } finally { - setIsLoading(false); } } + + setIsLoading(false); }; tryTelegramAuth(); }, [loginWithTelegram, navigate, t, getReturnUrl]); + // Manual retry for Telegram Mini App auth + const handleRetryTelegramAuth = async () => { + const initData = getTelegramInitData(); + if (!initData) { + setError(t('auth.telegramRequired')); + return; + } + + setError(''); + setIsLoading(true); + try { + await loginWithTelegram(initData); + navigate(getReturnUrl(), { replace: true }); + } catch (err) { + const status = (err as { response?: { status?: number } })?.response?.status; + console.warn('Telegram auth retry failed with status:', status); + setError(t('auth.telegramRetryFailed', 'Authorization failed. Close the app and try again.')); + } finally { + setIsLoading(false); + } + }; + const handleEmailSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); @@ -341,6 +372,34 @@ export default function Login() {
{t('auth.authenticating')}
+ ) : isTelegramWebApp && error ? ( ++ {t( + 'auth.telegramReopenHint', + 'If the problem persists, close and reopen the app', + )} +
+