More fixes for broken sessions

This commit is contained in:
Alexander Zinchuk 2021-06-23 17:44:06 +03:00
parent 1264f0d24a
commit 15e4cc25c0
4 changed files with 33 additions and 15 deletions

View File

@ -70,6 +70,7 @@ export async function init(_onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs)
console.log('[GramJs/client] CONNECTING'); console.log('[GramJs/client] CONNECTING');
} }
try {
await client.start({ await client.start({
phoneNumber: onRequestPhoneNumber, phoneNumber: onRequestPhoneNumber,
phoneCode: onRequestCode, phoneCode: onRequestCode,
@ -79,6 +80,14 @@ export async function init(_onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs)
onError: onAuthError, onError: onAuthError,
initialMethod: 'qrCode', initialMethod: 'qrCode',
}); });
} catch (err) {
onUpdate({
'@type': 'updateConnectionState',
connectionState: 'connectionStateBroken',
});
return;
}
if (DEBUG) { if (DEBUG) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console

View File

@ -244,7 +244,12 @@ class TelegramClient {
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async destroy() { async destroy() {
try {
await this.disconnect(); await this.disconnect();
} catch (err) {
// Do nothing
}
this.session.delete(); this.session.delete();
this._eventBuilders = []; this._eventBuilders = [];
} }

View File

@ -126,8 +126,12 @@ addReducer('saveSession', (global, actions, payload) => {
addReducer('signOut', () => { addReducer('signOut', () => {
(async () => { (async () => {
try {
await unsubscribe(); await unsubscribe();
await callApi('destroy'); await callApi('destroy');
} catch (err) {
// Do nothing
}
getDispatch().reset(); getDispatch().reset();
})(); })();

View File

@ -150,13 +150,13 @@ function onUpdateConnectionState(update: ApiUpdateConnectionState) {
} }
function onUpdateSession(update: ApiUpdateSession) { function onUpdateSession(update: ApiUpdateSession) {
const { sessionData } = update;
const { authRememberMe, authState } = getGlobal(); const { authRememberMe, authState } = getGlobal();
if (!authRememberMe || authState !== 'authorizationStateReady') {
if (!authRememberMe || (sessionData && authState !== 'authorizationStateReady')) {
return; return;
} }
const { sessionData } = update;
getDispatch().saveSession({ sessionData }); getDispatch().saveSession({ sessionData });
} }