diff --git a/pages/api/index.ts b/pages/api/index.ts new file mode 100644 index 0000000..c0cc894 --- /dev/null +++ b/pages/api/index.ts @@ -0,0 +1,22 @@ +import type { NextApiRequest, NextApiResponse } from 'next'; + +export default function handler( + req: NextApiRequest, + res: NextApiResponse, +) { + res + .status(200) + .json({ + latest: 0, + versions: [ + { + version: { + name: 'v1.0.0', + code: 1, + }, + path: '/v1', + deprecated: false, + } + ] + }); +} \ No newline at end of file diff --git a/pages/api/v1/auth.ts b/pages/api/v1/auth.ts deleted file mode 100644 index 4254569..0000000 --- a/pages/api/v1/auth.ts +++ /dev/null @@ -1,71 +0,0 @@ -import Database from '@/lib/Database'; -import { serialize } from 'cookie'; - -import type { NextApiRequest, NextApiResponse } from 'next'; - -type Data = { - sid: string | null; -}; - -type Error = { - error: string; -}; - -export default async function handler( - req: NextApiRequest, - res: NextApiResponse, -) { - const db = new Database(); - - const { code, state } = req.query; - - console.log(code, state); - - const discordApi = process.env.DISCORD_API!; - - try { - if (typeof code === 'string') { - const data = await fetch(`${discordApi}/oauth2/token`, { - method: 'POST', - body: new URLSearchParams({ - client_id: process.env.DISCORD_CLIENT!, - client_secret: process.env.DISCORD_SECRET!, - grant_type: 'authorization_code', - code, - redirect_uri: process.env.DISCORD_REDIRECT!, - }), - }); - - const json = await data.json(); - const sid = await db.createSession(json, req.headers['user-agent']); - - if (sid) { - const cookie = serialize('session', sid, { - secure: process.env.NODE_ENV === 'production', - sameSite: 'strict', - path: '/', - expires: new Date(Date.now() + json.expires_in * 1000), - }); - - res.setHeader('Set-Cookie', cookie); - - if ((state as string).startsWith('/')) - res.status(302).redirect(state as string); - else - res.status(400).json({ error: 'Invalid redirect uri in state!' }); - } - - return; - } - } catch (error) { - console.error(error); - - res.status(500).json( - { error: 'Internal Server Error' } - ); - } - - res.status(400).json( - { error: 'Invalid code' } - ); -} \ No newline at end of file diff --git a/pages/api/v1/server/info.ts b/pages/api/v1/server/info.ts deleted file mode 100644 index 754a8e6..0000000 --- a/pages/api/v1/server/info.ts +++ /dev/null @@ -1,61 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from 'next'; - -type Error = { - message: string; -}; - -interface ServerInfo { - version: string; - online: number; - worlds: string[]; -} - -import * as net from 'node:net'; - -export default function handler( - req: NextApiRequest, - res: NextApiResponse, -) { - try { - const mc_api = process.env.MC_API!.split(':'); - - const socket = net.createConnection({ - host: mc_api[0], - port: parseInt(mc_api[1]), - }, async () => { - const reqData = Buffer.alloc(1 + 4); - - reqData.writeInt8(0x00, 0); - reqData.writeUint32BE(0, 1); - - socket.write(reqData); - - socket.on('data', (data) => { - const packetId = data[0]; - const length = data.readUInt32BE(1); // unused but in case someone wants to verify the lenght :3 - - if (packetId !== 0x00) { - socket.end(); - - return res.status(500).json({ message: 'There was an error with the server.' }); - } - - const jsonData = data.toString('utf-8', 5); - - socket.end(); - - res.status(200).json(JSON.parse(jsonData)); - }); - - socket.on('error', (err) => { - console.error(err); - - socket.end(); - - res.status(500).json({ message: 'There was an error with the server.' }); - }); - }); - } catch (e) { - res.status(500).json({ message: 'There was an error with the server.' }); - } -} diff --git a/pages/api/v1/session.ts b/pages/api/v1/session.ts deleted file mode 100644 index bae72ba..0000000 --- a/pages/api/v1/session.ts +++ /dev/null @@ -1,48 +0,0 @@ -import Database from '@/lib/Database'; -import type { NextApiRequest, NextApiResponse } from 'next'; - -import { serialize } from 'cookie'; - -type Data = { - success: string; -}; - -interface Error { - error: string; -} - -export default async function handler( - req: NextApiRequest, - res: NextApiResponse, -) { - const db = new Database(); - const sid = req.cookies.session; - - if (!sid) - return res.status(401).json({ error: 'Unauthorized' }); - - const session = await db.getSession(sid!); - - if (!session) - return res.status(401).json({ error: 'Unauthorized' }); - - const user = await db.getUser(session.uid); - - if (!user) - return res.status(404).json({ error: 'Not Found' }); - - if (req.method === 'DELETE') { - db.deleteSession(sid!); - - res.setHeader('Set-Cookie', serialize('session', '', { - secure: process.env.NODE_ENV === 'production', - sameSite: 'strict', - path: '/', - expires: new Date(0), - })); - - return res.status(200).json({ success: 'Delete Session for ' + user.username }); - } - - res.status(405).json({ error: 'Method Not Allowed' }); -} \ No newline at end of file diff --git a/pages/api/v1/user/@me.ts b/pages/api/v1/user/@me.ts deleted file mode 100644 index f3fc985..0000000 --- a/pages/api/v1/user/@me.ts +++ /dev/null @@ -1,52 +0,0 @@ -import Database from '@/lib/Database'; -import type { NextApiRequest, NextApiResponse } from 'next'; - -type Data = { - id: number; - discord_id: string; - names: { - username: string; - global_name: string; - }; - email: string; - avatar?: string; - banner?: string; - accent_color?: number; - permissions: number; -}; - -interface Error { - error: string; -} - -export default async function handler( - req: NextApiRequest, - res: NextApiResponse, -) { - const db = new Database(); - const sid = req.cookies.session; - - if (!sid) - return res.status(401).json({ error: 'Unauthorized' }); - - const session = await db.getSession(sid!); - - if (!session) - return res.status(401).json({ error: 'Unauthorized' }); - - const user = await db.getUser(session.uid); - - res.status(200).json({ - id: user.id, - discord_id: user.did, - names: { - username: user.username, - global_name: user.global_name - }, - email: user.email, - avatar: user.avatar, - banner: user.banner, - accent_color: user.accent_color, - permissions: user.permissions - }); -} diff --git a/pages/api/v1/user/[username].ts b/pages/api/v1/user/[username].ts deleted file mode 100644 index d35d992..0000000 --- a/pages/api/v1/user/[username].ts +++ /dev/null @@ -1,69 +0,0 @@ -import Database from '@/lib/Database'; -import { isUserAdmin } from '@/utils/auth_util'; -import type { NextApiRequest, NextApiResponse } from 'next'; - -type Data = { - id: number; - discord_id: string; - names: { - username: string; - global_name: string; - }; - avatar?: string; - banner?: string; - accent_color?: number; - permissions: number; -}; - -interface Error { - error: string; -} - -export default async function handler( - req: NextApiRequest, - res: NextApiResponse, -) { - const db = new Database(); - - const user = await db.getUserUsername(req.query.username as string); - - if (!user) { - return res.status(404).json({ error: 'Not Found' }); - } - - // hehe only admins update users :trolley: - // also validation yeah uh... didn't have budget for that - // tech debt for the win - if (req.method === 'PATCH') { - const sid = req.cookies.session; - - const isAdmin = isUserAdmin(sid); - - if (!isAdmin) { - return res.status(401).json({ error: 'Unauthorized' }); - } - - const { permissions } = req.body; - - if (permissions) { - await db.updateUserPermissions(user.id, permissions); - } - - res.status(204).end(); - - return; - } - - res.status(200).json({ - id: user.id, - discord_id: user.did, - names: { - username: user.username, - global_name: user.global_name - }, - avatar: user.avatar, - banner: user.banner, - accent_color: user.accent_color, - permissions: user.permissions - }); -} diff --git a/pages/api/v1/users.ts b/pages/api/v1/users.ts deleted file mode 100644 index 422991d..0000000 --- a/pages/api/v1/users.ts +++ /dev/null @@ -1,24 +0,0 @@ -import Database from '@/lib/Database'; -import type { NextApiRequest, NextApiResponse } from 'next'; - -interface Response { - id: number; - did: string; - username: string; - global_name: string; - avatar?: string; - banner?: string; - accent_color?: number; - permissions: number; -} - -export default async function handler( - req: NextApiRequest, - res: NextApiResponse, -) { - const db = new Database(); - - const users = await db.getUsers(); - - res.status(200).json(users); -}