Created
October 30, 2016 18:03
-
-
Save mkozhukharenko/390b836f8d5c3d71688017afb708e45c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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