Skip to content

Instantly share code, notes, and snippets.

@mkozhukharenko
Created October 30, 2016 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkozhukharenko/390b836f8d5c3d71688017afb708e45c to your computer and use it in GitHub Desktop.
Save mkozhukharenko/390b836f8d5c3d71688017afb708e45c to your computer and use it in GitHub Desktop.
app.post('/',function (req, res, next) {
var body = req.body;
if (!body.name || !body.email || !body.password) {
return res.status(400).send("Missing username or email or password")
};
// case #1- if user was registered (socials platform) before -> just add a password;
User.findOneAndUpdate({
email: body.email
}, {
$set: {password: body.password}
}, (err, user) => {
if (!user) {
// case #2 - user has never been registering before -> create new one;
User.create(body, (err, user) => {
if (err && err.name === 'ValidationError') {
return res.status(400).send(err)
} else if (err) {
return res.status(500).send(err)
}
return res.send({ user: user })
});
} else {
return res.send({ user: body })
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment