Skip to content

Instantly share code, notes, and snippets.

@laere
Last active March 16, 2019 14:42
Show Gist options
  • Save laere/a77a8cc1695f48f89e53bdd2daa9d9b9 to your computer and use it in GitHub Desktop.
Save laere/a77a8cc1695f48f89e53bdd2daa9d9b9 to your computer and use it in GitHub Desktop.
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