fix: not process error (code: 401)
This commit is contained in:
@@ -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
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
.then(response => {
|
|
||||||
response.json().then(async data => {
|
// 处理非200响应状态码
|
||||||
if (response.ok) {
|
if (!response.ok) {
|
||||||
if (data.username == username) {
|
const text = await response.text(); // 先读取文本防止json解析失败
|
||||||
resolve('ok')
|
if (text === 'verify') {
|
||||||
|
console.error('Token错误')
|
||||||
|
return false;
|
||||||
} else {
|
} else {
|
||||||
utils.showErrorNotification('Token与用户名不符')
|
throw new Error(`HTTP Error: ${response.status} - ${text}`);
|
||||||
console.error('Token: ', token, ' Username: ', username)
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
utils.showErrorNotification('Token错误')
|
|
||||||
console.error('Error token: ', token)
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
})
|
const data = await response.json();
|
||||||
})
|
|
||||||
|
// 验证用户名匹配
|
||||||
|
if (data.username !== username) {
|
||||||
|
console.error('Token与用户名不符');
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 上传公钥
|
// 上传公钥
|
||||||
|
Reference in New Issue
Block a user