User stuff, config structure changes, web panel start, versioncheck fix, api start, recaptcha support for web panel

This commit is contained in:
2025-02-08 21:19:25 -05:00
parent bc3443b1dc
commit 73e9b72ad4
36 changed files with 5526 additions and 87 deletions

53
src/routes/user.ts Normal file
View File

@@ -0,0 +1,53 @@
import { APIUtils, getSrcIpDefault } from "../apiutils.ts";
// @ts-types = "npm:@types/express"
import express from "express";
import { User } from "../data/users.ts";
import Recaptcha from "../data/recaptcha.ts";
export const route = APIUtils.createRouter('/user');
type CreateUserBody = {
username: string,
password: string,
recaptcha: string
}
type CreatedUserResponse = {
uuid: string,
backupcode: string
}
const rateLimit = new APIUtils.RateLimiter(10, 1);
route.router.post('/create',
rateLimit.middle(),
express.json(),
APIUtils.checkBodyTypes<CreateUserBody>({ username: "test", password: "test", recaptcha: "test" }),
async (rq, rs) => {
const body = rq.body as CreateUserBody;
const recaptchaStatus = await Recaptcha.siteVerify(body.recaptcha, getSrcIpDefault(rq));
if (recaptchaStatus) {
const userinit = await User.init({ username: body.username, password: body.password });
if (userinit == null) {
rs.statusCode = 400;
rs.json(APIUtils.genericResponseFormat(true, "Username is already taken"));
} else {
const res: CreatedUserResponse = {
uuid: userinit.user.getUuid(),
backupcode: userinit.backupcode
}
rs.json(APIUtils.genericResponseFormat(false, "User created successfully", res));
}
}
else {
rs.statusCode = 400;
rs.json(APIUtils.genericResponseFormat(true, "ReCAPTCHA error"));
}
}
);