refactor(user): login function store token, username only after verifying token as true
This commit is contained in:
@@ -80,7 +80,7 @@ export class DevStarExtension {
|
||||
// 将devstar domain存在global state中
|
||||
context.globalState.update('devstarDomain', devstarDomain)
|
||||
|
||||
if (!this.user.isLogged()) {
|
||||
if (!await this.user.isLogged()) {
|
||||
// 如果没有用户登录,则直接登录;
|
||||
const res = await this.user.login(accessToken, devstarUsername)
|
||||
if (res === 'ok') {
|
||||
|
||||
35
src/user.ts
35
src/user.ts
@@ -3,7 +3,7 @@ import * as path from 'path';
|
||||
import * as os from 'os';
|
||||
import * as fs from 'fs';
|
||||
import DevstarAPIHandler from './devstar-api';
|
||||
import { showErrorNotification } from './utils';
|
||||
import * as utils from './utils';
|
||||
const {
|
||||
generateKeyPairSync,
|
||||
createHash
|
||||
@@ -82,29 +82,30 @@ export default class User {
|
||||
const res = await devstarAPIHandler.verifyToken(token, username)
|
||||
if (!res) {
|
||||
throw new Error('Token verification failed')
|
||||
}
|
||||
} else {
|
||||
// token与用户名验证通过
|
||||
// 插件登录:存储token与用户名
|
||||
this.setUserTokenToLocal(token)
|
||||
this.setUsernameToLocal(username)
|
||||
|
||||
// token与用户名验证通过
|
||||
// 插件登录:存储token与用户名
|
||||
this.setUserTokenToLocal(token)
|
||||
this.setUsernameToLocal(username)
|
||||
// 检查本地是否有用户所属公钥,没有则创建
|
||||
if (!this.existUserPublicKey()) {
|
||||
await this.createUserSSHKey()
|
||||
|
||||
// 检查本地是否有用户所属公钥,没有则创建
|
||||
if (!this.existUserPublicKey()) {
|
||||
await this.createUserSSHKey()
|
||||
|
||||
// 上传公钥
|
||||
const uploadResult = await devstarAPIHandler.uploadUserPublicKey(this)
|
||||
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) {
|
||||
console.error(error)
|
||||
await showErrorNotification('用户登录失败!')
|
||||
await utils.showErrorNotification('用户登录失败!')
|
||||
return 'login failed'
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user