diff --git a/src/components/TelegramConnectionModal.tsx b/src/components/TelegramConnectionModal.tsx index ec7e6bdcd..5e626f1a3 100644 --- a/src/components/TelegramConnectionModal.tsx +++ b/src/components/TelegramConnectionModal.tsx @@ -52,12 +52,17 @@ const TelegramConnectionModal = ({ isOpen, onClose, onComplete }: TelegramConnec if (connectionStatus !== 'connected') { await dispatch(connectTelegram()).unwrap(); } - // Check if already authenticated - const authCheck = await dispatch(checkAuthStatus()).unwrap(); - if (authCheck) { - onComplete(); - onClose(); - return; + // Check if already authenticated (but don't fail if not authenticated) + try { + const authCheck = await dispatch(checkAuthStatus()).unwrap(); + if (authCheck) { + onComplete(); + onClose(); + return; + } + } catch (err) { + // If checkAuthStatus fails, we're not authenticated - continue with QR flow + console.log('Not authenticated, proceeding with QR code flow'); } // Start QR code flow startQrCodeFlow(); @@ -134,7 +139,12 @@ const TelegramConnectionModal = ({ isOpen, onClose, onComplete }: TelegramConnec // Authentication successful setIsAuthenticating(false); - await dispatch(checkAuthStatus()).unwrap(); + try { + await dispatch(checkAuthStatus()).unwrap(); + } catch (err) { + // Even if checkAuthStatus fails, we know we just authenticated + console.warn('checkAuthStatus failed after authentication, but continuing:', err); + } dispatch(setAuthStatus('authenticated')); onComplete(); onClose(); diff --git a/src/store/telegramSlice.ts b/src/store/telegramSlice.ts index 9014480a9..05a28957e 100644 --- a/src/store/telegramSlice.ts +++ b/src/store/telegramSlice.ts @@ -192,14 +192,30 @@ export const checkAuthStatus = createAsyncThunk( async (_, { rejectWithValue }) => { try { const client = mtprotoService.getClient(); + + // First check if we're authorized without making API calls const isAuthorized = await client.checkAuthorization(); - if (isAuthorized) { + if (!isAuthorized) { + return null; + } + + // Only call getMe() if we're authorized + try { const me = await client.getMe(); return me; + } catch (error) { + // If getMe() fails, we're not actually authorized + // This can happen if the session is invalid + console.warn('getMe() failed, user not authenticated:', error); + return null; } - return null; } catch (error) { + // If checkAuthorization itself fails, we're definitely not authenticated + // Don't treat this as an error, just return null + if (error instanceof Error && error.message.includes('AUTH_KEY_UNREGISTERED')) { + return null; + } return rejectWithValue( error instanceof Error ? error.message : 'Failed to check auth status' );