From 9b58fda0fd888f1b6499c421f9dae1ea9f535981 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Tue, 13 Apr 2021 23:19:09 +0300 Subject: [PATCH] Sign out when session is terminated from another application --- src/api/gramjs/methods/client.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/api/gramjs/methods/client.ts b/src/api/gramjs/methods/client.ts index 0d3553789..dbfdc32d8 100644 --- a/src/api/gramjs/methods/client.ts +++ b/src/api/gramjs/methods/client.ts @@ -97,6 +97,8 @@ export async function destroy() { function handleGramJsUpdate(update: any) { if (update instanceof connection.UpdateConnectionState) { isConnected = update.state === connection.UpdateConnectionState.connected; + } else if (update instanceof GramJs.UpdatesTooLong) { + void handleTerminatedSession(); } } @@ -231,3 +233,18 @@ function injectUpdateEntities(result: GramJs.Updates | GramJs.UpdatesCombined) { } }); } + +async function handleTerminatedSession() { + try { + await invokeRequest(new GramJs.users.GetFullUser({ + id: new GramJs.InputUserSelf(), + }), undefined, true); + } catch (err) { + if (err.message === 'AUTH_KEY_UNREGISTERED') { + onUpdate({ + '@type': 'updateConnectionState', + connectionState: 'connectionStateBroken', + }); + } + } +}