token cache

This commit is contained in:
2025-02-26 23:59:42 +08:00
parent 26c6248d10
commit c01820d1c6
12 changed files with 433 additions and 44 deletions

View File

@@ -6,11 +6,40 @@ import inquirer from 'inquirer';
import { runApp } from '../app-run.ts';
import { chalk } from '@/module/chalk.ts';
import { loginInCommand } from '@/module/login/login-by-web.ts';
export const saveToken = async (token: string) => {
const baseURL = getBaseURL();
const res = await runApp({ path: 'config', key: 'saveToken', payload: { baseURL, token } });
if (res.code !== 200) {
console.log('Set token failed', res.message || '');
}
};
export const switchToken = async (baseURL: string) => {
const res = await runApp({ path: 'config', key: 'switchToken', payload: { baseURL } });
if (res.code !== 200 && res.code !== 404) {
console.log('switch token failed', res.message || '');
}
return res;
};
export const deleteToken = async (baseURL: string) => {
const res = await runApp({ path: 'config', key: 'deleteToken', payload: { baseURL } });
if (res.code !== 200) {
console.log('delete token failed', res.message || '');
}
return res;
};
export const getTokenList = async () => {
const res = await runApp({ path: 'config', key: 'getTokenList' });
if (res.code !== 200) {
console.log('get token list failed', res.message || '');
}
return res;
};
// 定义login命令支持 `-u` 和 `-p` 参数来输入用户名和密码
const loginCommand = new Command('login')
.description('Login to the application')
.option('-u, --username <username>', 'Specify username')
.option('-p, --password <password>', 'Specify password')
.option('-f, --force', 'Force login')
.option('-w, --web', 'Login on the web')
.action(async (options) => {
const config = getConfig();
@@ -54,6 +83,7 @@ const loginCommand = new Command('login')
if (res.code === 200) {
const { token } = res.data;
writeConfig({ ...config, token });
saveToken(token);
console.log('welcome', username);
} else {
console.log('登录失败', res.message || '');
@@ -100,6 +130,7 @@ const switchOrgCommand = new Command('switch').argument('<username>', 'Switch to
const token = res.data.token;
writeConfig({ ...config, token });
console.log(`Switch ${username} Success`);
saveToken(token);
await showMe();
} else {
console.log(`Switch ${username} Failed`, res.message || '');