From 051bd2952487bba7c15e2f75830d88713efab628 Mon Sep 17 00:00:00 2001 From: SamYSF <98792230+SamYSF@users.noreply.github.com> Date: Sun, 24 Mar 2024 22:24:53 +0800 Subject: [PATCH] fix: use current input params to login (#22) --- src/oauth.js | 61 +++++++++++++++++++++++++++++++++++++++++----------- src/popup.js | 26 ++++++++++++++++------ src/sdk.js | 5 ++++- 3 files changed, 71 insertions(+), 21 deletions(-) diff --git a/src/oauth.js b/src/oauth.js index 9a5ddee..db56889 100644 --- a/src/oauth.js +++ b/src/oauth.js @@ -29,22 +29,57 @@ chrome.storage.sync.get("endpoint", ({endpoint}) => { } }); -const sdk = new Sdk(CasdoorConfig); +let sdk; + +function getStorageData(keys) { + return new Promise((resolve, reject) => { + chrome.storage.sync.get(keys, (data) => { + if (chrome.runtime.lastError) { + reject(chrome.runtime.lastError.message); + } else { + resolve(data); + } + }); + }); +} + +function initInputParamSdk(){ + return new Promise((resolve, reject) => { + Promise.all([getStorageData("applicationName"), getStorageData("endpoint")]) + .then(([applicationData, endpointData]) => { + const applicationName = applicationData.applicationName; + const endpoint = endpointData.endpoint; + + if (applicationName && endpoint) { + CasdoorConfig.applicationName = applicationName; + CasdoorConfig.endpoint = endpoint; + } + sdk = new Sdk(CasdoorConfig); + resolve(sdk); + }) + .catch((error) => { + console.error("init SDK failed:", error); + reject(error); + }); + }); +} // eslint-disable-next-line no-unused-vars function login() { - sdk.login((accessToken) => { - if (accessToken) { - chrome.storage.sync.set({accessToken}, () => { - sdk - .getUserProfile(accessToken) - .then((userProfile) => displayUserProfile(userProfile)); - }); - setInputDisabledState(true, "endpoint", "applicationName"); - } else { - alert("Login failed!"); - } - }); + initInputParamSdk().then((sdk) => { + sdk.login((accessToken) => { + if (accessToken) { + chrome.storage.sync.set({accessToken}, () => { + sdk + .getUserProfile(accessToken) + .then((userProfile) => displayUserProfile(userProfile)); + }); + setInputDisabledState(true, "endpoint", "applicationName"); + } else { + alert("Login failed!"); + } + }); + }) } // eslint-disable-next-line no-unused-vars diff --git a/src/popup.js b/src/popup.js index b91aaf6..13facbe 100644 --- a/src/popup.js +++ b/src/popup.js @@ -47,7 +47,11 @@ document.addEventListener("DOMContentLoaded", function() { if (accessToken) { logout(); } else { - login(); + setInput() + .then(login()) + .catch(error => { + console.error('Error login:', error); + }); } }); }); @@ -60,9 +64,6 @@ document.addEventListener("DOMContentLoaded", function() { applicationNameDom.value = applicationName; } }); - applicationNameDom.addEventListener("input", function(e) { - chrome.storage.sync.set({applicationName: e.target.value}); - }); const endpointDom = document.getElementById("endpoint"); chrome.storage.sync.get("endpoint", ({endpoint}) => { @@ -70,7 +71,18 @@ document.addEventListener("DOMContentLoaded", function() { endpointDom.value = endpoint; } }); - endpointDom.addEventListener("input", function(e) { - chrome.storage.sync.set({endpoint: e.target.value}); - }); }); + +function setInput() { + return new Promise((resolve, reject) => { + const endpointDom = document.getElementById("endpoint"); + const applicationNameDom = document.getElementById("applicationName"); + chrome.storage.sync.set({ endpoint: endpointDom.value, applicationName: applicationNameDom.value }, function() { + if (chrome.runtime.lastError) { + reject(chrome.runtime.lastError.message); + } else { + resolve(); + } + }); + }); +} \ No newline at end of file diff --git a/src/sdk.js b/src/sdk.js index d2d3fee..7b0da62 100644 --- a/src/sdk.js +++ b/src/sdk.js @@ -125,6 +125,9 @@ class Sdk { }; return fetch(applicationUrl, requestConfig) .then((response) => response.json()) - .catch((error) => console.error(error)); + .catch((error) => { + console.error(error) + alert("failed to get application"); + }); } }