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,15 +70,24 @@ export async function init(_onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs)
console.log('[GramJs/client] CONNECTING');
}
await client.start({
phoneNumber: onRequestPhoneNumber,
phoneCode: onRequestCode,
password: onRequestPassword,
firstAndLastNames: onRequestRegistration,
qrCode: onRequestQrCode,
onError: onAuthError,
initialMethod: 'qrCode',
});
try {
await client.start({
phoneNumber: onRequestPhoneNumber,
phoneCode: onRequestCode,
password: onRequestPassword,
firstAndLastNames: onRequestRegistration,
qrCode: onRequestQrCode,
onError: onAuthError,
initialMethod: 'qrCode',
});
} catch (err) {
onUpdate({
'@type': 'updateConnectionState',
connectionState: 'connectionStateBroken',
});
return;
}
if (DEBUG) {
// eslint-disable-next-line no-console

View File

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

View File

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

View File

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