Create Register endpoint;

This commit is contained in:
Stefan-5422
2022-03-23 12:59:33 +01:00
parent d7f97b49e7
commit 4df48360b5
2 changed files with 75 additions and 0 deletions
+67
View File
@@ -0,0 +1,67 @@
import express from "express";
import { Request, Response } from "express";
import prisma from "../lib/prisma.lib";
import { Role } from "@prisma/client";
import { createDefaultError, createDefaultSucces } from "../lib/standardResponse";
import argon2 from "argon2";
interface registerBody {
name: string;
password: string;
role: string;
organization?: number;
}
export interface authJwt {
id: number;
role: string;
}
const ROLES: readonly string[] = [Role.ADMIN, Role.USER];
export const register = async (req: Request, res: Response) => {
const { name, password, role, organization } = req.body || {};
console.log(name);
if (
!(
typeof name === "string" &&
typeof password === "string" &&
ROLES.includes(role) &&
typeof organization === "number"
)
) {
return res.status(400).json(
createDefaultError("Request body does not match required parameters", {
name: "string",
password: "string",
role: "USER | ADMIN",
organization: "int",
})
);
}
const hash = await argon2.hash("salty" + password + "salty");
if (!(await prisma.organization.findUnique({ where: { id: organization } }))) {
return res.status(404).json(createDefaultError(`Could not find organization with id ${organization}`, {}));
}
const user = await prisma.user.create({
data: {
email: name,
passwordSalt: "salty",
passwordHash: hash,
role: role as Role,
organization: { connect: { id: organization } || undefined },
},
select: {
id: true,
email: true,
role: true,
},
});
res.status(201).json(createDefaultSucces("User created succesfully", user));
};
+8
View File
@@ -0,0 +1,8 @@
import { Router } from "express";
import { register } from "../controller/auth.controller";
const router = Router();
router.post("/register", register);
export default router;