feat: some add auth
This commit is contained in:
@@ -4,7 +4,11 @@ import { CustomError } from '@abearxiong/router';
|
||||
import { agentManger } from '@kevisual/ai-lang';
|
||||
import { v4 } from 'uuid';
|
||||
app
|
||||
.route('agent', 'list')
|
||||
.route({
|
||||
path: 'agent',
|
||||
key: 'list',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const agents = await AiAgent.findAll({
|
||||
order: [['updatedAt', 'DESC']],
|
||||
|
||||
@@ -7,6 +7,7 @@ app
|
||||
.route({
|
||||
path: 'chat-history',
|
||||
key: 'list',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const chatPrompt = await ChatHistory.findAll({
|
||||
|
||||
@@ -6,6 +6,7 @@ app
|
||||
.route({
|
||||
path: 'chat-session',
|
||||
key: 'list',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const chatSession = await ChatSession.findAll({
|
||||
|
||||
@@ -40,8 +40,10 @@ app
|
||||
const add = app.route({
|
||||
path: 'container',
|
||||
key: 'update',
|
||||
middleware: ['auth'],
|
||||
});
|
||||
add.run = async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const data = ctx.query.data;
|
||||
|
||||
const _data: Container = {
|
||||
@@ -72,6 +74,7 @@ add.run = async (ctx) => {
|
||||
...container,
|
||||
source: '',
|
||||
sourceType: '',
|
||||
uid: tokenUser.id,
|
||||
});
|
||||
} catch (e) {
|
||||
console.log('error', e);
|
||||
|
||||
1
src/routes/github/index.ts
Normal file
1
src/routes/github/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
import './list.ts';
|
||||
51
src/routes/github/lib/get-token.ts
Normal file
51
src/routes/github/lib/get-token.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import fetch from 'node-fetch';
|
||||
import { useConfig } from '@abearxiong/use-config';
|
||||
|
||||
type GithubConfig = {
|
||||
clientId: string;
|
||||
clientSecret: string;
|
||||
redirect_uri: string;
|
||||
};
|
||||
const { github } = useConfig<{ github: GithubConfig }>();
|
||||
// 获取 GitHub access_token 的函数
|
||||
async function getGithubToken(github: GithubConfig, code) {
|
||||
const { clientId, clientSecret, redirect_uri } = github;
|
||||
// 设置请求 URL 和参数
|
||||
const tokenUrl = 'https://github.com/login/oauth/access_token';
|
||||
const params = {
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
code: code,
|
||||
redirect_uri: redirect_uri,
|
||||
};
|
||||
|
||||
try {
|
||||
// 发送 POST 请求获取 access_token
|
||||
const response = await fetch(tokenUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Accept: 'application/json',
|
||||
},
|
||||
body: JSON.stringify(params),
|
||||
});
|
||||
|
||||
// 解析响应 JSON
|
||||
const data: any = await response.json();
|
||||
|
||||
if (data.access_token) {
|
||||
console.log('Access Token:', data.access_token);
|
||||
return data.access_token;
|
||||
} else {
|
||||
console.error('Error:', data.error || 'Failed to get access token');
|
||||
return null;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching access token:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export const getAccessToken = async (code?: string) => {
|
||||
return getGithubToken(github, code);
|
||||
};
|
||||
48
src/routes/github/list.ts
Normal file
48
src/routes/github/list.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { app } from '@/app.ts';
|
||||
import { CustomError } from '@abearxiong/router';
|
||||
import { getAccessToken } from './lib/get-token.ts';
|
||||
import { GithubModel } from './models/github.ts';
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'github',
|
||||
key: 'token',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const github = await GithubModel.findOne({
|
||||
where: {
|
||||
uid: tokenUser.id,
|
||||
},
|
||||
logging: false,
|
||||
});
|
||||
if (github) {
|
||||
ctx.body = {
|
||||
githubToken: github.githubToken,
|
||||
};
|
||||
return;
|
||||
}
|
||||
const { code } = ctx.query;
|
||||
if (!code) {
|
||||
throw new CustomError(400, 'code is required');
|
||||
}
|
||||
try {
|
||||
console.log('get access token from github, code:', code);
|
||||
const token = await getAccessToken(code);
|
||||
if (!token) {
|
||||
throw new CustomError(500, 'Failed to get access token');
|
||||
}
|
||||
await GithubModel.create({
|
||||
uid: tokenUser.id,
|
||||
githubToken: token,
|
||||
});
|
||||
ctx.body = {
|
||||
githubToken: token,
|
||||
};
|
||||
} catch (e) {
|
||||
console.error('get access token from github error:', e);
|
||||
throw new CustomError(500, 'Failed to get access token');
|
||||
}
|
||||
})
|
||||
.addTo(app);
|
||||
45
src/routes/github/models/github.ts
Normal file
45
src/routes/github/models/github.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { sequelize } from '../../../modules/sequelize.ts';
|
||||
import { DataTypes, Model } from 'sequelize';
|
||||
|
||||
export type Github = Partial<InstanceType<typeof GithubModel>>;
|
||||
|
||||
/**
|
||||
* 用户代码容器
|
||||
*/
|
||||
export class GithubModel extends Model {
|
||||
declare id: string;
|
||||
declare title: string;
|
||||
declare githubToken: string;
|
||||
declare uid: string;
|
||||
}
|
||||
GithubModel.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
primaryKey: true,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
comment: 'id',
|
||||
},
|
||||
title: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
githubToken: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
uid: {
|
||||
type: DataTypes.UUID,
|
||||
allowNull: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
tableName: 'kv_github',
|
||||
paranoid: true,
|
||||
},
|
||||
);
|
||||
|
||||
GithubModel.sync({ alter: true, logging: false }).catch((e) => {
|
||||
console.error('GithubModel sync', e);
|
||||
});
|
||||
@@ -13,3 +13,5 @@ import './user/index.ts';
|
||||
import './chat-prompt/index.ts';
|
||||
|
||||
import './chat-history/index.ts';
|
||||
|
||||
import './github/index.ts';
|
||||
|
||||
@@ -47,9 +47,11 @@ app
|
||||
.route({
|
||||
path: 'page',
|
||||
key: 'update',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { data, id, title, type, description } = ctx.query.data;
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
if (id) {
|
||||
const page = await PageModel.findByPk(id);
|
||||
if (page) {
|
||||
@@ -59,7 +61,7 @@ app
|
||||
throw new CustomError('page not found');
|
||||
}
|
||||
} else if (data) {
|
||||
const page = await PageModel.create({ data, title, type, description });
|
||||
const page = await PageModel.create({ data, title, type, description, uid: tokenUser.id });
|
||||
ctx.body = page;
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user