import {Elysia} from 'elysia' import userController from './User/Controller' import adminController from './admin/Controller' import passwordController from './password/Controller' import type { User } from './interfaces/user_interface' import db from './Database' import { cors } from '@elysiajs/cors' const app = new Elysia() .derive(async ({ headers }) => { const auth = headers['authorization'] if (!auth) { return {} } const bearer = auth?.startsWith('Bearer ') ? auth.slice(7) : null const msg = db.query(`select users.id, users.name, users.administrator from users JOIN tokens ON users.id = tokens.user_id WHERE tokens.token = ?;`) .get(bearer) console.log(msg) if(msg == null) { throw new Response("Invalid token", { status: 401 }) return {} } const user: User = { id: msg.id, username: msg.username, name: msg.name, admin: msg.administrator } return { user } // return user here instead of bearer }) .get('/', ({ user }) => user) .group('/user', (app) => app.use(userController)) .group('/admin', (app) => app.use(adminController)) .group('/password', (app) => app.use(passwordController)) .use(cors()) .listen(3000)