From c3318d93e626ab61c47a9c6611f473008558806e Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Tue, 19 Apr 2022 15:12:23 +0200 Subject: [PATCH] Fix infinite redirect to Auth screen --- src/api/gramjs/methods/client.ts | 1 + src/lib/gramjs/client/TelegramClient.js | 2 +- src/lib/gramjs/client/auth.ts | 5 +++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/api/gramjs/methods/client.ts b/src/api/gramjs/methods/client.ts index 423ee528..aecdb0b8 100644 --- a/src/api/gramjs/methods/client.ts +++ b/src/api/gramjs/methods/client.ts @@ -94,6 +94,7 @@ export async function init(_onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs) qrCode: onRequestQrCode, onError: onAuthError, initialMethod: platform === 'iOS' || platform === 'Android' ? 'phoneNumber' : 'qrCode', + shouldThrowIfUnauthorized: Boolean(sessionData), }); } catch (err: any) { // eslint-disable-next-line no-console diff --git a/src/lib/gramjs/client/TelegramClient.js b/src/lib/gramjs/client/TelegramClient.js index 4f126505..ebbde289 100644 --- a/src/lib/gramjs/client/TelegramClient.js +++ b/src/lib/gramjs/client/TelegramClient.js @@ -836,7 +836,7 @@ class TelegramClient { await this.connect(); } - if (await checkAuthorization(this)) { + if (await checkAuthorization(this, authParams.shouldThrowIfUnauthorized)) { return; } diff --git a/src/lib/gramjs/client/auth.ts b/src/lib/gramjs/client/auth.ts index 5d81bf9b..cabacbba 100644 --- a/src/lib/gramjs/client/auth.ts +++ b/src/lib/gramjs/client/auth.ts @@ -13,6 +13,7 @@ export interface UserAuthParams { onError: (err: Error) => void; forceSMS?: boolean; initialMethod?: 'phoneNumber' | 'qrCode'; + shouldThrowIfUnauthorized?: boolean; } export interface BotAuthParams { @@ -49,12 +50,12 @@ export async function authFlow( client._log.info('Signed in successfully as', utils.getDisplayName(me)); } -export async function checkAuthorization(client: TelegramClient) { +export async function checkAuthorization(client: TelegramClient, shouldThrow = false) { try { await client.invoke(new Api.updates.GetState()); return true; } catch (e: any) { - if (e.message === 'Disconnect') throw e; + if (e.message === 'Disconnect' || shouldThrow) throw e; return false; } }