PasswordBox/backend/src/index.ts

41 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-09-15 23:18:42 +01:00
import {Elysia} from 'elysia'
import userController from './User/Controller'
import adminController from './admin/Controller'
2024-10-04 10:49:39 +01:00
import passwordController from './password/Controller'
import type { User } from './interfaces/user_interface'
import db from './Database'
import { cors } from '@elysiajs/cors'
2024-09-15 23:18:42 +01:00
const app = new Elysia()
2024-10-04 10:49:39 +01:00
.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)
2024-09-15 23:18:42 +01:00
.group('/user', (app) => app.use(userController))
.group('/admin', (app) => app.use(adminController))
2024-10-04 10:49:39 +01:00
.group('/password', (app) => app.use(passwordController))
.use(cors())
2024-09-15 23:18:42 +01:00
.listen(3000)