diff --git a/routes/api/users.js b/routes/api/users.js index f4b6393..99857ac 100644 --- a/routes/api/users.js +++ b/routes/api/users.js @@ -20,6 +20,48 @@ const validator = require('../../validations/userValidations') // cosole.log(req.body); //}); +router.post('/register', async (req, res) => { + + try { + + const isValidated = validator.registerValidation(req.body); + + if (isValidated.error) return res.status(400).send({ error: isValidated.error.details[0].message }); + + const { email, age, name, password } = req.body; + + const user = await User.findOne({ email }); + + if (user) return res.status(400).json({ email: 'Email already exists' }); + + const salt = bcrypt.genSaltSync(10); + + const hashedPassword = bcrypt.hashSync(password, salt); + + const newUser = new User({ + + name, + + password: hashedPassword, + + email, + + age, + + }); + + await User.create(newUser); + + res.json({ msg: 'User created successfully', data: newUser }); + + } catch (error) { + + res.status(422).send({ error: 'Can not create user' }); + + } + +}); + router.post('/', (req,res) => { try { const isValidated = validator.createValidation(req.body)