Files
opengifame/pages/api/user/create.ts
2022-10-05 22:05:13 +01:00

31 lines
749 B
TypeScript

import type {NextApiRequest, NextApiResponse} from "next";
import prisma from "@lib/prismadb";
import {logger} from "@lib/logger";
import {hashPassword} from "@lib/crypt";
import {omit} from "lodash";
export default async function handle(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method === "POST") {
logger.debug("creating user", {
...req.body,
password: hashPassword(req.body.password),
});
const user = await prisma.user.create({
data: {
email: req.body.email,
password: await hashPassword(req.body.password)
}
});
res.json(omit(user, "password"));
} else {
throw new Error(
`The HTTP ${req.method} method is not supported at this route.`
);
}
}