fix: clear all cached auth state on Telegram MiniApp retry

When users hit 'Try Again' after auth failure, clear ALL cached state
(tokens, SDK launch params, initData, zustand persist, tg_user_id)
to prevent stale token loops. Applies to both Login and TelegramRedirect
retry handlers.
This commit is contained in:
Fringg
2026-03-22 07:24:45 +03:00
parent 1538879f97
commit 3e27472c8a
2 changed files with 19 additions and 3 deletions

View File

@@ -15,7 +15,7 @@ import {
type BrandingInfo, type BrandingInfo,
type EmailAuthEnabled, type EmailAuthEnabled,
} from '../api/branding'; } from '../api/branding';
import { getAndClearReturnUrl } from '../utils/token'; import { getAndClearReturnUrl, tokenStorage } from '../utils/token';
import { isInTelegramWebApp, getTelegramInitData, useTelegramSDK } from '../hooks/useTelegramSDK'; import { isInTelegramWebApp, getTelegramInitData, useTelegramSDK } from '../hooks/useTelegramSDK';
import { closeMiniApp } from '@telegram-apps/sdk-react'; import { closeMiniApp } from '@telegram-apps/sdk-react';
import LanguageSwitcher from '../components/LanguageSwitcher'; import LanguageSwitcher from '../components/LanguageSwitcher';
@@ -205,11 +205,18 @@ export default function Login() {
}, [isAuthInitializing, loginWithTelegram, navigate, t, getReturnUrl]); }, [isAuthInitializing, loginWithTelegram, navigate, t, getReturnUrl]);
const handleRetryTelegramAuth = () => { const handleRetryTelegramAuth = () => {
// Clear ALL cached auth state to prevent stale token/initData loops
tokenStorage.clearTokens();
sessionStorage.removeItem('tapps/launchParams');
sessionStorage.removeItem('telegram_init_data');
localStorage.removeItem('cabinet-auth');
localStorage.removeItem('tg_user_id');
try { try {
sessionStorage.removeItem('tapps/launchParams'); // Close miniapp — Telegram will provide fresh initData on reopen
sessionStorage.removeItem('telegram_init_data');
closeMiniApp(); closeMiniApp();
} catch { } catch {
// If closeMiniApp fails, force a clean page reload
window.location.reload(); window.location.reload();
} }
}; };

View File

@@ -6,6 +6,7 @@ import { useAuthStore } from '../store/auth';
import { useShallow } from 'zustand/shallow'; import { useShallow } from 'zustand/shallow';
import { brandingApi } from '../api/branding'; import { brandingApi } from '../api/branding';
import { isInTelegramWebApp, getTelegramInitData } from '../hooks/useTelegramSDK'; import { isInTelegramWebApp, getTelegramInitData } from '../hooks/useTelegramSDK';
import { tokenStorage } from '../utils/token';
// Validate redirect URL to prevent open redirect attacks // Validate redirect URL to prevent open redirect attacks
const getSafeRedirectUrl = (url: string | null): string => { const getSafeRedirectUrl = (url: string | null): string => {
@@ -117,6 +118,14 @@ export default function TelegramRedirect() {
const newCount = retryCount + 1; const newCount = retryCount + 1;
setRetryCount(newCount); setRetryCount(newCount);
sessionStorage.setItem(RETRY_COUNT_KEY, String(newCount)); sessionStorage.setItem(RETRY_COUNT_KEY, String(newCount));
// Clear all cached auth state to prevent stale token/initData loops
tokenStorage.clearTokens();
sessionStorage.removeItem('tapps/launchParams');
sessionStorage.removeItem('telegram_init_data');
localStorage.removeItem('cabinet-auth');
localStorage.removeItem('tg_user_id');
setStatus('loading'); setStatus('loading');
setErrorMessage(''); setErrorMessage('');
window.location.reload(); window.location.reload();