mirror of
https://github.com/chillpadclub/bedolaga-cabinet.git
synced 2026-07-28 17:43:47 +00:00
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:
@@ -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();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
Reference in New Issue
Block a user