import { randomBytes } from "crypto"; import pool from "./database.js"; import type { RowDataPacket } from "mysql2"; import type scope from "../../types/api/scope"; export async function createAPIToken( user: string, scope: scope[], name: string, ) { const token = randomBytes(32).toString("hex"); const [insert] = await pool.execute( "INSERT INTO `token` (`user`, `scope`, `name`, `token`) VALUES (?, ?, ?, ?);", [user, scope.join(","), name, token], ); return token; } export async function getScopeAPIToken(token: string) { const [tokenData] = await pool.execute( "SELECT * FROM `token` WHERE token = ?", [token] ); const scope: scope[] = tokenData[0].scope.split(","); return scope; }