refactor(user): login function store token, username only after verifying token as true

This commit is contained in:
Levi Yan
2025-06-22 16:12:50 +08:00
parent ed7ee8cce3
commit 6e75693220
2 changed files with 19 additions and 18 deletions

View File

@@ -80,7 +80,7 @@ export class DevStarExtension {
// 将devstar domain存在global state中 // 将devstar domain存在global state中
context.globalState.update('devstarDomain', devstarDomain) context.globalState.update('devstarDomain', devstarDomain)
if (!this.user.isLogged()) { if (!await this.user.isLogged()) {
// 如果没有用户登录,则直接登录; // 如果没有用户登录,则直接登录;
const res = await this.user.login(accessToken, devstarUsername) const res = await this.user.login(accessToken, devstarUsername)
if (res === 'ok') { if (res === 'ok') {

View File

@@ -3,7 +3,7 @@ import * as path from 'path';
import * as os from 'os'; import * as os from 'os';
import * as fs from 'fs'; import * as fs from 'fs';
import DevstarAPIHandler from './devstar-api'; import DevstarAPIHandler from './devstar-api';
import { showErrorNotification } from './utils'; import * as utils from './utils';
const { const {
generateKeyPairSync, generateKeyPairSync,
createHash createHash
@@ -82,29 +82,30 @@ export default class User {
const res = await devstarAPIHandler.verifyToken(token, username) const res = await devstarAPIHandler.verifyToken(token, username)
if (!res) { if (!res) {
throw new Error('Token verification failed') throw new Error('Token verification failed')
} } else {
// token与用户名验证通过
// 插件登录存储token与用户名
this.setUserTokenToLocal(token)
this.setUsernameToLocal(username)
// token与用户名验证通过 // 检查本地是否有用户所属公钥,没有则创建
// 插件登录存储token与用户名 if (!this.existUserPublicKey()) {
this.setUserTokenToLocal(token) await this.createUserSSHKey()
this.setUsernameToLocal(username)
// 检查本地是否有用户所属公钥,没有则创建 // 上传公钥
if (!this.existUserPublicKey()) { const uploadResult = await devstarAPIHandler.uploadUserPublicKey(this)
await this.createUserSSHKey() if (uploadResult !== 'ok') {
throw new Error('Upload user public key failed')
// 上传公钥 }
const uploadResult = await devstarAPIHandler.uploadUserPublicKey(this)
if (uploadResult !== 'ok') {
throw new Error('Upload user public key failed')
} }
vscode.window.showInformationMessage(vscode.l10n.t('User login successfully!'))
return 'ok'
} }
vscode.window.showInformationMessage(vscode.l10n.t('User login successfully!'))
return 'ok'
} catch (error) { } catch (error) {
console.error(error) console.error(error)
await showErrorNotification('用户登录失败!') await utils.showErrorNotification('用户登录失败!')
return 'login failed' return 'login failed'
} }
} }