Fix infinite redirect to Auth screen

This commit is contained in:
Alexander Zinchuk 2022-04-19 15:12:23 +02:00
parent fe8f147a59
commit c3318d93e6
3 changed files with 5 additions and 3 deletions

View File

@ -94,6 +94,7 @@ export async function init(_onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs)
qrCode: onRequestQrCode, qrCode: onRequestQrCode,
onError: onAuthError, onError: onAuthError,
initialMethod: platform === 'iOS' || platform === 'Android' ? 'phoneNumber' : 'qrCode', initialMethod: platform === 'iOS' || platform === 'Android' ? 'phoneNumber' : 'qrCode',
shouldThrowIfUnauthorized: Boolean(sessionData),
}); });
} catch (err: any) { } catch (err: any) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console

View File

@ -836,7 +836,7 @@ class TelegramClient {
await this.connect(); await this.connect();
} }
if (await checkAuthorization(this)) { if (await checkAuthorization(this, authParams.shouldThrowIfUnauthorized)) {
return; return;
} }

View File

@ -13,6 +13,7 @@ export interface UserAuthParams {
onError: (err: Error) => void; onError: (err: Error) => void;
forceSMS?: boolean; forceSMS?: boolean;
initialMethod?: 'phoneNumber' | 'qrCode'; initialMethod?: 'phoneNumber' | 'qrCode';
shouldThrowIfUnauthorized?: boolean;
} }
export interface BotAuthParams { export interface BotAuthParams {
@ -49,12 +50,12 @@ export async function authFlow(
client._log.info('Signed in successfully as', utils.getDisplayName(me)); 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 { try {
await client.invoke(new Api.updates.GetState()); await client.invoke(new Api.updates.GetState());
return true; return true;
} catch (e: any) { } catch (e: any) {
if (e.message === 'Disconnect') throw e; if (e.message === 'Disconnect' || shouldThrow) throw e;
return false; return false;
} }
} }