Skip to content

Commit

Permalink
Merge pull request #39 from kentibs/feat/newletter
Browse files Browse the repository at this point in the history
feat: signup endpoint for normal users
  • Loading branch information
Tibz-Dankan authored Jan 27, 2024
2 parents 99942d6 + fac6154 commit e87f1f3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
42 changes: 42 additions & 0 deletions src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,48 @@ const validateGender = (gender: string): boolean => {
};

export const signUp = asyncHandler(
async (req: Request, res: Response, next: NextFunction) => {
const firstName = req.body.firstName as string;
const lastName = req.body.lastName as string;
const phone = req.body.phoneNumber as string;
const email = req.body.email as string;
const gender = req.body.gender as string;
const password = req.body.password as string;

if (!email || !phone || !firstName || !lastName || !password || !gender) {
return next(new AppError("Please fill out all fields", 400));
}
if (!email.includes("@")) {
return next(new AppError("Please provide a valid email", 400));
}
if (!validateGender(gender)) {
return next(new AppError("Please provide a valid gender", 400));
}
const user = await User.findFirst({
where: { email: { equals: email } },
});
if (user) return next(new AppError("Email already taken", 400));

const salt = await genSalt(10);
req.body.password = await hash(req.body.password, salt);

const newUser = await User.create({
data: req.body,
select: {
userId: true,
firstName: true,
lastName: true,
phoneNumber: true,
role: true,
imageUrl: true,
},
});

authenticate(newUser, 201, res);
}
);

export const signUpAdmin = asyncHandler(
async (req: Request, res: Response, next: NextFunction) => {
const firstName = req.body.firstName as string;
const lastName = req.body.lastName as string;
Expand Down
4 changes: 2 additions & 2 deletions src/routes/auth/userRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from "express";
import {
signUp,
signUpAdmin,
signIn,
resetPassword,
forgotPassword,
Expand All @@ -15,7 +15,7 @@ import { validateSignupToken } from "../../controllers/tokenController";

const router = express.Router();

router.post("/signup", validateSignupToken, signUp);
router.post("/signup", validateSignupToken, signUpAdmin);
router.post("/signin", signIn);
router.post("/forgot-password", forgotPassword);
router.patch("/reset-password/:token", resetPassword);
Expand Down

0 comments on commit e87f1f3

Please sign in to comment.