Skip to content

Commit

Permalink
Fix token refreshing
Browse files Browse the repository at this point in the history
unnamed777 committed Jan 15, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 03bbdff commit 684b132
Showing 3 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bx24dev",
"version": "0.4.1",
"version": "0.4.2",
"description": "Bitrix24 Dev extension",
"author": "Andrey Neyman <[email protected]>",
"engines": {
2 changes: 1 addition & 1 deletion src/extension/index.js
Original file line number Diff line number Diff line change
@@ -110,4 +110,4 @@ const initBX24 = (authId, authData) => {
}
}
});
})();
})();
50 changes: 34 additions & 16 deletions src/lib/Manager/ExtensionManager.js
Original file line number Diff line number Diff line change
@@ -48,35 +48,28 @@ class ExtensionManager extends AbstractManager {
const instanceId = payload.payload.authId;
console.log('Manager.onMessageGetAuth()', instanceId, payload);

if (!this.instances[instanceId]) {
console.log('Instance isn\'t in memory, try to hydrate');
// If there is no instance in memory, try to hydrate it from session data
let result = await this.getSessionInstanceData();
const data = result.instanceData[instanceId];
let instance = await this.getInstance(instanceId);

if (!data) {
console.error(`Authorization with ID ${instanceId} not found`);
return;
}

this.hydrateInstance(data);
if (instance === null) {
return;
}

//await sleep(5000);
console.log('Data will be sent as response', this.instances[instanceId].getData());
sendResponse(this.instances[instanceId].getData());
console.log('Data will be sent as response', instance.getData());
sendResponse(instance.getData());
}

async onMessageRefreshAuth(payload, sender, sendResponse) {
const authId = payload.payload.authId;
console.log('Manager.onMessageRefreshAuth()', authId);

if (!this.instances[authId]) {
console.error(`Authorization with ID ${authId} not found`);
let instance = await this.getInstance(instanceId);

if (instance === null) {
return;
}

let result = await this.instances[authId].refreshAuth();
let result = await instance.refreshAuth();
console.log('Manager.onMessageRefreshAuth(), result', result);
sendResponse(result);
}
@@ -286,6 +279,31 @@ class ExtensionManager extends AbstractManager {
this.hydrateInstance(data);
}
}

/**
* Returns instance from memory. If nothing found,
* try to find it in session data and hydrate object.
*
* @param instanceId
* @returns {Promise<AbstractInstance|null>}
*/
async getInstance(instanceId) {
if (!this.instances[instanceId]) {
console.log('Instance isn\'t in memory, try to hydrate');
// If there is no instance in memory, try to hydrate it from session data
let result = await this.getSessionInstanceData();
const data = result.instanceData[instanceId];

if (!data) {
console.error(`Authorization with ID ${instanceId} not found`);
return null;
}

this.hydrateInstance(data);
}

return this.instances[instanceId];
}
}

const manager = new ExtensionManager(messageListener);

0 comments on commit 684b132

Please sign in to comment.