diff --git a/src/server/routes/api/v0/primaryApi.ts b/src/server/routes/api/v0/primaryApi.ts index 2b5ed29..b44b717 100644 --- a/src/server/routes/api/v0/primaryApi.ts +++ b/src/server/routes/api/v0/primaryApi.ts @@ -1,6 +1,4 @@ -import bodyParser from "body-parser" import { Hono } from "hono" -import {stream as startHonoStream} from "hono/streaming" import * as Accounts from "../../../lib/accounts.js" import * as auth from "../../../lib/auth.js" import RangeParser, { type Range } from "range-parser" @@ -8,12 +6,11 @@ import ServeError from "../../../lib/errors.js" import Files, { WebError } from "../../../lib/files.js" import { getAccount, requiresPermissions } from "../../../lib/middleware.js" import {Readable} from "node:stream" -import {ReadableStream as StreamWebReadable} from "node:stream/web" +import type {ReadableStream as StreamWebReadable} from "node:stream/web" import formidable from "formidable" import { HttpBindings } from "@hono/node-server" import pkg from "../../../../../package.json" assert {type: "json"} import { type StatusCode } from "hono/utils/http-status" -import { EventEmitter } from "node:events" export let primaryApi = new Hono<{ Variables: { account: Accounts.Account diff --git a/src/server/routes/api/v1/account.ts b/src/server/routes/api/v1/account.ts index 92ca1fa..21ae146 100644 --- a/src/server/routes/api/v1/account.ts +++ b/src/server/routes/api/v1/account.ts @@ -30,37 +30,8 @@ const router = new Hono<{ router.use(getAccount) export default function (files: Files) { - router.post("/login", async (ctx, res) => { - const body = await ctx.req.json() - if ( - typeof body.username != "string" || - typeof body.password != "string" - ) { - ServeError(ctx, 400, "please provide a username or password") - return - } - if (auth.validate(getCookie(ctx, "auth")!)) { - ServeError(ctx, 400, "you are already logged in") - return - } - - const account = Accounts.getFromUsername(body.username) - - if (!account || !Accounts.password.check(account.id, body.password)) { - ServeError(ctx, 400, "username or password incorrect") - return - } - setCookie(ctx, "auth", auth.create(account.id, 3 * 24 * 60 * 60 * 1000), { - path: "/", - sameSite: "Strict", - secure: true, - httpOnly: true - }) - ctx.status(200) - }) - - router.post("/create", async (ctx) => { + router.post("/", async (ctx) => { const body = await ctx.req.json() if (!Configuration.accounts.registrationEnabled) { return ServeError(ctx, 403, "account registration disabled") @@ -115,15 +86,6 @@ export default function (files: Files) { }) }) - router.post("/logout", (ctx) => { - if (!auth.validate(getCookie(ctx, "auth")!)) { - return ServeError(ctx, 401, "not logged in") - } - - auth.invalidate(getCookie(ctx, "auth")!) - return ctx.text("logged out") - }) - router.put( "/dfv", requiresAccount, diff --git a/src/server/routes/api/v1/api.json b/src/server/routes/api/v1/api.json index 694f259..cb186e4 100644 --- a/src/server/routes/api/v1/api.json +++ b/src/server/routes/api/v1/api.json @@ -6,6 +6,7 @@ "admin", "public", "file", + "session", { "file": "customization", "to": "/account/customization" diff --git a/src/server/routes/api/v1/session.ts b/src/server/routes/api/v1/session.ts new file mode 100644 index 0000000..3299282 --- /dev/null +++ b/src/server/routes/api/v1/session.ts @@ -0,0 +1,66 @@ +// Modules + + +import { Hono } from "hono" +import { getCookie, setCookie } from "hono/cookie" + +// Libs + +import Files, { id_check_regex } from "../../../lib/files.js" +import * as Accounts from "../../../lib/accounts.js" +import * as auth from "../../../lib/auth.js" +import { + getAccount, +} from "../../../lib/middleware.js" +import ServeError from "../../../lib/errors.js" + +const router = new Hono<{ + Variables: { + account: Accounts.Account + } +}>() + +router.use(getAccount) + +export default function (files: Files) { + router.post("/", async (ctx, res) => { + const body = await ctx.req.json() + if ( + typeof body.username != "string" || + typeof body.password != "string" + ) { + ServeError(ctx, 400, "please provide a username or password") + return + } + + if (auth.validate(getCookie(ctx, "auth")!)) { + ServeError(ctx, 400, "you are already logged in") + return + } + + const account = Accounts.getFromUsername(body.username) + + if (!account || !Accounts.password.check(account.id, body.password)) { + ServeError(ctx, 400, "username or password incorrect") + return + } + setCookie(ctx, "auth", auth.create(account.id, 3 * 24 * 60 * 60 * 1000), { + path: "/", + sameSite: "Strict", + secure: true, + httpOnly: true + }) + ctx.status(200) + }) + + router.delete("/", (ctx) => { + if (!auth.validate(getCookie(ctx, "auth")!)) { + return ServeError(ctx, 401, "not logged in") + } + + auth.invalidate(getCookie(ctx, "auth")!) + return ctx.text("logged out") + }) + + return router +}