feat: 更新多个依赖版本,添加客户端认证模块及缓存功能
This commit is contained in:
@@ -46,7 +46,6 @@ export class AssistantApp extends Manager {
|
||||
if (token) {
|
||||
console.log('用户已登录,正在初始化远程应用连接...');
|
||||
await manager.initRemoteApp({ token });
|
||||
await manager.initRemoteApp();
|
||||
await manager.initRouterProxyLightApp();
|
||||
}
|
||||
await manager.initRouterProxyApp();
|
||||
@@ -212,7 +211,7 @@ export class AssistantApp extends Manager {
|
||||
const query = new Query({ url });
|
||||
try {
|
||||
initApi({
|
||||
router: this.mainApp,
|
||||
router: this.mainApp as any,
|
||||
item: {
|
||||
type: 'api',
|
||||
api: {
|
||||
|
||||
6
assistant/src/module/client-auth/cache-auth.ts
Normal file
6
assistant/src/module/client-auth/cache-auth.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { LRUCache } from 'lru-cache'
|
||||
|
||||
export const authCache = new LRUCache<string, any>({
|
||||
max: 10000, // 最大缓存数量
|
||||
ttl: 1000 * 60 * 60 * 24 * 7, // 缓存过期时间,单位为毫秒,这里设置为7天
|
||||
});
|
||||
58
assistant/src/module/client-auth/index.ts
Normal file
58
assistant/src/module/client-auth/index.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { Query } from "@kevisual/query";
|
||||
import { authCache } from './cache-auth.ts';
|
||||
|
||||
const getTokenUser = async (token: string) => {
|
||||
const query = new Query();
|
||||
const res = await query.post({
|
||||
path: 'user',
|
||||
key: 'me',
|
||||
token: token,
|
||||
});
|
||||
return res;
|
||||
}
|
||||
export const getTokenUserCache = async (token: string) => {
|
||||
const tokenUser = await authCache.get(token);
|
||||
if (tokenUser) {
|
||||
return {
|
||||
code: 200,
|
||||
data: tokenUser,
|
||||
};
|
||||
}
|
||||
const res = await getTokenUser(token);
|
||||
if (res.code === 200) {
|
||||
authCache.set(token, res.data);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
export const checkAuth = async (ctx: any, isAdmin = false) => {
|
||||
const token = ctx.query.token;
|
||||
if (!token) {
|
||||
return {
|
||||
code: 401,
|
||||
message: '未登录',
|
||||
}
|
||||
}
|
||||
// 鉴权代理
|
||||
let tokenUser = await authCache.get(token);
|
||||
if (!tokenUser) {
|
||||
const tokenUserRes = await getTokenUser(token);
|
||||
if (tokenUserRes.code !== 200) {
|
||||
return {
|
||||
code: tokenUserRes.code,
|
||||
message: '验证失败' + tokenUserRes.message,
|
||||
}
|
||||
} else {
|
||||
tokenUser = tokenUserRes.data;
|
||||
}
|
||||
authCache.set(token, tokenUser);
|
||||
}
|
||||
ctx.state = {
|
||||
...ctx.state,
|
||||
token,
|
||||
tokenUser,
|
||||
};
|
||||
return {
|
||||
code: 200,
|
||||
data: { tokenUser, token }
|
||||
}
|
||||
};
|
||||
14
assistant/src/module/client-auth/package.json
Normal file
14
assistant/src/module/client-auth/package.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "client-auth",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
|
||||
"license": "MIT",
|
||||
"packageManager": "pnpm@10.31.0",
|
||||
"type": "module"
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@kevisual/remote-app",
|
||||
"version": "0.0.4",
|
||||
"version": "0.0.6",
|
||||
"description": "",
|
||||
"main": "dist/app.js",
|
||||
"scripts": {
|
||||
@@ -19,10 +19,10 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"eventemitter3": "^5.0",
|
||||
"@kevisual/router": "^0.0.70"
|
||||
"@kevisual/router": "^0.0.90"
|
||||
},
|
||||
"author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
|
||||
"license": "MIT",
|
||||
"packageManager": "pnpm@10.28.2",
|
||||
"packageManager": "pnpm@10.31.0",
|
||||
"type": "module"
|
||||
}
|
||||
@@ -44,7 +44,7 @@ export class RemoteApp {
|
||||
constructor(opts?: RemoteAppOptions) {
|
||||
this.mainApp = opts?.app;
|
||||
const token = opts.token;
|
||||
const url = opts.url;
|
||||
const url = opts.url || 'https://kevisual.cn/ws/proxy';
|
||||
const id = opts.id;
|
||||
const username = opts.username;
|
||||
this.username = username;
|
||||
|
||||
Reference in New Issue
Block a user