Created
July 5, 2020 06:44
-
-
Save suhas86/5b0ebfbce6c57d6d54cf75481e2dca83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express, { Request, Response } from "express"; | |
import { body, check, validationResult } from "express-validator"; | |
const router = express.Router(); | |
router.post( | |
"/api/users/signup", | |
[ | |
body("firstName") | |
.trim() | |
.not() | |
.isEmpty() | |
.withMessage("First Name is required"), | |
body("email").isEmail().withMessage("Email must be valid"), | |
body("password") | |
.trim() | |
.isLength({ min: 6 }) | |
.withMessage("Password must be minimum of 6 characters"), | |
check("password").custom((val, { req }) => { | |
if (val !== req.body.confirmPassword) { | |
throw new Error("Passwords don't match"); | |
} else { | |
return val; | |
} | |
}), | |
], | |
async (req: Request, res: Response) => { | |
const errors = validationResult(req); | |
if (!errors.isEmpty()) { | |
return res.status(400).send(errors.array()); | |
} | |
return res.send({}); | |
} | |
); | |
export { router as SignupRoute }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment