fix: not process error (code: 401)

This commit is contained in:
Levi Yan
2025-03-23 21:59:57 +08:00
parent a858f80164
commit c47739a718

View File

@@ -17,31 +17,40 @@ export default class DevstarAPIHandler {
} }
} }
public async verifyToken(token: string, username: string): Promise<string> { public async verifyToken(token: string, username: string): Promise<boolean> {
return new Promise(async (resolve) => { try {
fetch(this.devstarDomain + `/api/devcontainer/user`, { const response = await fetch(this.devstarDomain + `/api/devcontainer/user`, {
method: 'GET', method: 'GET',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': 'token ' + token 'Authorization': 'token ' + token
}
});
// 处理非200响应状态码
if (!response.ok) {
const text = await response.text(); // 先读取文本防止json解析失败
if (text === 'verify') {
console.error('Token错误')
return false;
} else {
throw new Error(`HTTP Error: ${response.status} - ${text}`);
}
} }
})
.then(response => { const data = await response.json();
response.json().then(async data => {
if (response.ok) { // 验证用户名匹配
if (data.username == username) { if (data.username !== username) {
resolve('ok') console.error('Token与用户名不符');
} else { return false
utils.showErrorNotification('Token与用户名不符') }
console.error('Token: ', token, ' Username: ', username)
} return true;
} else { } catch (error) {
utils.showErrorNotification('Token错误') console.error(error)
console.error('Error token: ', token) return false
} }
})
})
})
} }
// 上传公钥 // 上传公钥