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> {
 | 
			
		||||
    return new Promise(async (resolve) => {
 | 
			
		||||
      fetch(this.devstarDomain + `/api/devcontainer/user`, {
 | 
			
		||||
        method: 'GET',
 | 
			
		||||
        headers: {
 | 
			
		||||
          'Content-Type': 'application/json',
 | 
			
		||||
          'Authorization': 'token ' + token
 | 
			
		||||
  public async verifyToken(token: string, username: string): Promise<boolean> {
 | 
			
		||||
    try {
 | 
			
		||||
        const response = await fetch(this.devstarDomain + `/api/devcontainer/user`, {
 | 
			
		||||
          method: 'GET',
 | 
			
		||||
          headers: {
 | 
			
		||||
            'Content-Type': 'application/json',
 | 
			
		||||
            '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 => {
 | 
			
		||||
          response.json().then(async data => {
 | 
			
		||||
            if (response.ok) {
 | 
			
		||||
              if (data.username == username) {
 | 
			
		||||
                resolve('ok')
 | 
			
		||||
              } else {
 | 
			
		||||
                utils.showErrorNotification('Token与用户名不符')
 | 
			
		||||
                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