Skip to content

Commit

Permalink
fix: use current input params to login (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamYSF authored Mar 24, 2024
1 parent 37c04a5 commit 051bd29
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 21 deletions.
61 changes: 48 additions & 13 deletions src/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 19 additions & 7 deletions src/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ document.addEventListener("DOMContentLoaded", function() {
if (accessToken) {
logout();
} else {
login();
setInput()
.then(login())
.catch(error => {
console.error('Error login:', error);
});
}
});
});
Expand All @@ -60,17 +64,25 @@ 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}) => {
if (endpoint) {
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();
}
});
});
}
5 changes: 4 additions & 1 deletion src/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
}
}

0 comments on commit 051bd29

Please sign in to comment.