52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
import url from "node:url";
|
|
import path from "node:path";
|
|
import { defineEventHandler, setResponseStatus, readBody } from "h3";
|
|
// eslint-disable-next-line import/default
|
|
import PrismaMigrate from "@prisma/migrate";
|
|
|
|
import { database } from "../utils/database";
|
|
import { isFirstRun } from "./firstRun.get";
|
|
import { getPasswordHash } from "./login.post";
|
|
import Snowflake from "~/utils/snowflake";
|
|
|
|
import { createError } from "#imports";
|
|
|
|
export default defineEventHandler(async (e) => {
|
|
if (!isFirstRun()) {
|
|
setResponseStatus(e, 404);
|
|
return null;
|
|
}
|
|
|
|
const body = await readBody(e);
|
|
if (typeof body !== "object") throw createError({ message: "Invalid body", statusCode: 400 });
|
|
const username = body.username;
|
|
if (typeof username !== "string") throw createError({ message: "username is not string", statusCode: 400 });
|
|
const password = body.password;
|
|
if (typeof password !== "string") throw createError({ message: "password is not string", statusCode: 400 });
|
|
const email = body.email;
|
|
if (typeof email !== "string") throw createError({ message: "email is not string", statusCode: 400 });
|
|
|
|
const dbPushParam = [
|
|
"--force-reset",
|
|
"--skip-generate",
|
|
];
|
|
|
|
if (!import.meta.dev ?? true) {
|
|
const mainPath = path.dirname(url.fileURLToPath(import.meta.url));
|
|
dbPushParam.push(
|
|
"--schema",
|
|
`${mainPath}/node_modules/.prisma/client/schema.prisma`,
|
|
);
|
|
}
|
|
|
|
await (new PrismaMigrate.DbPush()).parse(dbPushParam);
|
|
await database.user.create({
|
|
data: {
|
|
id: new Snowflake().state,
|
|
username,
|
|
email,
|
|
password: getPasswordHash(password),
|
|
},
|
|
});
|
|
return null;
|
|
});
|