Last active
March 16, 2019 14:42
-
-
Save laere/a77a8cc1695f48f89e53bdd2daa9d9b9 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
router.post("/register", (req, res) => { | |
const userProps = req.body; | |
const { email } = userProps; | |
User.findOne({ email }) | |
.then(user => { | |
if (user) { | |
return res.status(400).json({ errormessage: "User already exists." }); | |
} else { | |
const avatar = gravatar.url(email, { | |
s: "200", // Size | |
r: "pg", // Rating | |
d: "mm" // Default | |
}); | |
const newUser = User.create({ ...userProps, avatar }); | |
Joi.validate(newUser, userValidationSchema, { | |
abortEarly: false | |
}); | |
bcrypt.genSalt(10, (err, salt) => { | |
bcrypt.hash(newUser.password, salt, (err, hash) => { | |
if (err) throw err; | |
newUser.password = hash; | |
newUser | |
.save() | |
.then(user => res.json(user)) | |
.catch(err => res.status(400).json(err)); | |
}); | |
}); | |
} | |
}) | |
.catch(e => console.log(e)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment