refactor: extract code related to logging into login function

This commit is contained in:
Levi Yan
2025-03-23 20:31:31 +08:00
parent a55ee0cf90
commit a858f80164
2 changed files with 52 additions and 55 deletions

View File

@@ -37,33 +37,12 @@ export class DevStarExtension {
if (access_token && devstar_username) {
if (!this.user.isLogged()) {
// 如果没有用户登录,则直接登录;
await devstarAPIHandler.verifyToken(access_token, devstar_username)
.then(async (res) => {
const res = await this.user.login(access_token, devstar_username)
if (res === 'ok') {
// token与用户名验证通过
// 插件登录存储token与用户名
this.user.setUserTokenToLocal(access_token)
this.user.setUsernameToLocal(devstar_username)
// 检查本地是否有用户所属公钥,没有则创建
if (!this.user.existUserPublicKey()) {
await this.user.createUserSSHKey()
.then(async () => {
// 上传公钥
await devstarAPIHandler.uploadUserPublicKey(this.user)
.then(async (res) => {
if (res === "ok") {
// 打开项目
await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path)
}
})
})
} else {
// 公钥已上传,直接打开项目
await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path)
console.error(res)
}
}
})
} else if (devstar_username === this.user.getUsernameFromLocal()) {
// 如果同用户已经登录,则忽略;
// 直接打开项目
@@ -73,33 +52,13 @@ export class DevStarExtension {
const selection = await vscode.window.showWarningMessage(`已登录用户:${this.user.getUsernameFromLocal()},是否切换用户?`,
'Yes', 'No',);
if (selection === 'Yes') {
await devstarAPIHandler.verifyToken(access_token, devstar_username)
.then(async (res) => {
// 如果没有用户登录,则直接登录;
const res = await this.user.login(access_token, devstar_username)
if (res === 'ok') {
// token与用户名验证通过
// 插件登录存储token与用户名
this.user.setUserTokenToLocal(access_token)
this.user.setUsernameToLocal(devstar_username)
// 检查本地是否有用户所属公钥,没有则创建
if (!this.user.existUserPublicKey()) {
await this.user.createUserSSHKey()
.then(async () => {
// 上传公钥
await devstarAPIHandler.uploadUserPublicKey(this.user)
.then(async (res) => {
if (res === "ok") {
// 打开项目
await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path)
}
})
})
} else {
// 公钥已上传,直接打开项目
await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path)
console.error(res)
}
}
})
} else if (selection === 'No') {
await openProjectWithoutLogging(container_host, container_port, container_username, project_path);
}

View File

@@ -2,6 +2,7 @@ import * as vscode from 'vscode';
import * as path from 'path';
import * as os from 'os';
import * as fs from 'fs';
import DevstarAPIHandler from './devstar-api';
const {
generateKeyPairSync,
createHash
@@ -31,6 +32,43 @@ export default class User {
this.devstarHostname = parsedUrl.hostname.replace(/\./g, '_'); //提取hostname并用下划线替换.
}
public async login(token: string, username: string): Promise<string> {
const devstarAPIHandler = new DevstarAPIHandler()
try {
const res = await devstarAPIHandler.verifyToken(token, username)
if (res !== 'ok') {
throw new Error('Token verification failed')
}
// token与用户名验证通过
// 插件登录存储token与用户名
this.setUserTokenToLocal(token)
this.setUsernameToLocal(username)
// 检查本地是否有用户所属公钥,没有则创建
if (!this.existUserPublicKey()) {
await this.createUserSSHKey()
// 上传公钥
const uploadResult = await devstarAPIHandler.uploadUserPublicKey(this)
if (uploadResult !== 'ok') {
throw new Error('Upload user public key failed')
}
}
return 'ok'
} catch (error) {
console.error(error)
return 'login failed'
}
}
public logout() {
this.setUserTokenToLocal("")
this.setUsernameToLocal("")
}
public isLogged() {
var existUsername = false;
var existUserToken = false;