From 2f6811e15be401813b0fbee2cacd3ba7ef62c4ee Mon Sep 17 00:00:00 2001 From: Gusted Date: Fri, 28 May 2021 21:11:30 +0200 Subject: [PATCH] Store USw Token --- background/style-manager.js | 9 ++++++--- background/token-manager.js | 12 ++++++------ edit/edit.js | 7 +++++-- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/background/style-manager.js b/background/style-manager.js index 6df2e9cf..bcd897d9 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -48,6 +48,7 @@ const styleMan = (() => { name: style => `ID: ${style.id}`, _id: () => uuidv4(), _rev: () => Date.now(), + _uswToken: () => "", }; const DELETE_IF_NULL = ['id', 'customName', 'md5Url', 'originalMd5']; /** @type {Promise|boolean} will be `true` to avoid wasting a microtask tick on each `await` */ @@ -357,8 +358,10 @@ const styleMan = (() => { if (!style.id) { return; } - const result = await tokenMan.getToken('userstylesworld', true); - console.log(result); + const result_token = await tokenMan.getToken('userstylesworld', true, style.id); + style._uswToken = result_token; + await saveStyle(style) + broadcastStyleUpdated(style, 'updateLinking'); }); } @@ -437,7 +440,7 @@ const styleMan = (() => { style.id = newId; } uuidIndex.set(style._id, style.id); - API.sync.put(style._id, style._rev); + API.sync.put(style._id, style._rev, style._uswToken); } async function saveStyle(style) { diff --git a/background/token-manager.js b/background/token-manager.js index f1e4f550..9205a930 100644 --- a/background/token-manager.js +++ b/background/token-manager.js @@ -63,11 +63,11 @@ const tokenMan = (() => { return { - buildKeys(name) { + buildKeys(name, styleId) { const k = { - TOKEN: `secure/token/${name}/token`, - EXPIRE: `secure/token/${name}/expire`, - REFRESH: `secure/token/${name}/refresh`, + TOKEN: `secure/token/${name}/${styleId ? `${styleId}/`: ''}token`, + EXPIRE: `secure/token/${name}/${styleId ? `${styleId}/`: ''}expire`, + REFRESH: `secure/token/${name}/${styleId ? `${styleId}/`: ''}refresh`, }; k.LIST = Object.values(k); return k; @@ -77,8 +77,8 @@ const tokenMan = (() => { return AUTH[name].clientId; }, - async getToken(name, interactive) { - const k = tokenMan.buildKeys(name); + async getToken(name, interactive, styleId) { + const k = tokenMan.buildKeys(name, styleId); const obj = await chromeLocal.get(k.LIST); if (obj[k.TOKEN]) { if (!obj[k.EXPIRE] || Date.now() < obj[k.EXPIRE]) { diff --git a/edit/edit.js b/edit/edit.js index 6d20a3b8..752740a7 100644 --- a/edit/edit.js +++ b/edit/edit.js @@ -54,11 +54,14 @@ msg.onExtension(request => { const {style} = request; switch (request.method) { case 'styleUpdated': - if (editor.style.id === style.id && - !['editPreview', 'editPreviewEnd', 'editSave', 'config'].includes(request.reason)) { + if (editor.style.id === style.id) + if (!['editPreview', 'editPreviewEnd', 'editSave', 'config'].includes(request.reason)) { Promise.resolve(request.codeIsUpdated === false ? style : API.styles.get(style.id)) .then(newStyle => editor.replaceStyle(newStyle, request.codeIsUpdated)); } + if (request.reason === 'updateLinking') { + console.log(editor.style._uswToken) + } break; case 'styleDeleted': if (editor.style.id === style.id) {