Skip to content
Snippets Groups Projects
Commit 5a7f9137 authored by Leander Tolksdorf's avatar Leander Tolksdorf
Browse files

add create account route

parent d41e024b
No related branches found
No related tags found
No related merge requests found
import bcrypt from "bcrypt";
import { Request, Response } from "express";
import Worker from "../db/models/Worker";
export const createAccountController = async (req: Request, res: Response) => {
try {
if (!(res.locals.user.role === "coordinator")) {
return res
.status(403)
.json({ success: false, error: "MustBeCoordinator" });
}
const { first_name, last_name, email, password, role } = req.body;
const account = await Worker.findAll({
where: {
email: email,
},
});
if (account.length > 0) {
return res
.status(409)
.json({ success: false, error: "AccountAlreadyExists" });
}
const hashedPassword = await bcrypt.hash(password, 10);
const newAccount = await Worker.create({
email,
firstName: first_name,
lastName: last_name,
password: hashedPassword,
role,
});
return res.status(201).send({
success: true,
account: {
first_name: newAccount.firstName,
last_name: newAccount.lastName,
email: newAccount.email,
role: newAccount.role,
},
});
} catch {
return res.status(500).json({ success: false, error: "serverError" });
}
};
import { Router } from "express";
import { body } from "express-validator";
import handleValidationResult from "../middleware/handleValidationResult";
import validateToken from "../middleware/validateToken";
import { createAccountController } from "./accounts.controllers";
const accountsRouter = Router();
accountsRouter.post(
"/api/accounts/",
body("first_name").not().isEmpty(),
body("last_name").not().isEmpty(),
body("email").isEmail().normalizeEmail(),
body("role").isIn(["coordinator", "boatManager"]),
body("password").isLength({ min: 6 }),
handleValidationResult,
validateToken,
createAccountController
);
export default accountsRouter;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment