import './routes/index.ts'; import { app } from './app.ts'; import { User } from './models/user.ts'; import { addAuth } from '@kevisual/code-center-module/models'; addAuth(app); app .route({ path: 'auth', key: 'admin', id: 'auth-admin', isDebug: true, middleware: ['auth'], }) .define(async (ctx) => { const tokenUser = ctx.state.tokenUser; if (!tokenUser) { ctx.throw(401, 'No User For authorized'); } try { const user = await User.findOne({ where: { id: tokenUser.id, }, }); if (!user) { ctx.throw(404, 'user not found'); } user.setTokenUser(tokenUser); const orgs = await user.getOrgs(); if (orgs.includes('admin')) { ctx.body = 'admin'; } else { ctx.throw(403, 'forbidden'); } } catch (e) { console.error(`auth-admin error`, e); console.error('tokenUser', tokenUser?.id, tokenUser?.username, tokenUser?.uid); ctx.throw(500, e.message); } }) .addTo(app); app .route({ path: 'auth-check', key: 'admin', id: 'check-auth-admin', middleware: ['auth'], }) .define(async (ctx) => { const tokenUser = ctx.state.tokenUser; if (!tokenUser) { ctx.throw(401, 'No User For authorized'); } try { const user = await User.findOne({ where: { id: tokenUser.id, }, }); if (!user) { ctx.throw(404, 'user not found'); } user.setTokenUser(tokenUser); const orgs = await user.getOrgs(); if (orgs.includes('admin')) { ctx.body = 'admin'; ctx.state.tokenAdmin = { id: user.id, username: user.username, orgs, }; return; } ctx.body = 'not admin'; } catch (e) { console.error(`auth-admin error`, e); console.error('tokenUser', tokenUser?.id, tokenUser?.username, tokenUser?.uid); ctx.throw(500, e.message); } }) .addTo(app); app .route({ path: 'test', key: 'test', }) .define(async (ctx) => { ctx.body = app.router.routes.map((item) => { return { path: item.path, key: item.key, description: item.description, validator: item.validator, // schema: item.schema, }; }); }) .addTo(app);