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,
onError: onAuthError,
initialMethod: platform === 'iOS' || platform === 'Android' ? 'phoneNumber' : 'qrCode',
shouldThrowIfUnauthorized: Boolean(sessionData),
});
} catch (err: any) {
// eslint-disable-next-line no-console

View File

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

View File

@ -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;
}
}