refactor(getUserTokenFromVSCode): separate the verifying process from the function

This commit is contained in:
Levi Yan
2024-10-23 14:41:22 +08:00
parent bac497fabf
commit f8544e5282

View File

@@ -201,6 +201,20 @@ header("Allow: GET, POST, OPTIONS, PUT, DELETE");
window.onload = async function() { window.onload = async function() {
await getUserTokenFromVSCode() await getUserTokenFromVSCode()
.then(async userToken => {
// verify user token
await verifyToken(userToken)
.then(result => {
// initialize user token
USERTOKEN = userToken
})
.catch(error => {
console.error('Error in verifying token:', error)
})
})
.catch(error => {
console.error("Failed to get user token from vscode: ", error)
})
if (USERTOKEN) { if (USERTOKEN) {
loadPageModules() loadPageModules()
@@ -215,25 +229,19 @@ header("Allow: GET, POST, OPTIONS, PUT, DELETE");
} }
async function getUserTokenFromVSCode() { async function getUserTokenFromVSCode() {
await communicateVSCodeByWebview('getUserToken', null) return new Promise(async (resolve, reject) => {
.then(async data => { await communicateVSCodeByWebview('getUserToken', null)
const userToken = data.userToken .then(async data => {
if (userToken === '') { const userToken = data.userToken
// do nothing
} else {
// verify user token
await verifyToken(userToken)
.then(result => {
USERTOKEN = userToken
})
.catch(error => {
console.error('Error in verifying token:', error)
})
}
}) if (userToken === undefined) {
.catch(error => { reject("userToken is undefined")
console.error('Failed to get user token: ', error) }
resolve(userToken)
})
.catch(error => {
reject(error)
})
}) })
} }