Skip to content

Instantly share code, notes, and snippets.

@mkozhukharenko
Last active April 11, 2017 12:36
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/a19c341f61795b67390f393af36a50df to your computer and use it in GitHub Desktop.
Save mkozhukharenko/a19c341f61795b67390f393af36a50df to your computer and use it in GitHub Desktop.
Expressks auth with promise .js
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 }
})
.then((user) => {
if (!user) return User.create(body);
return Promise.resolve(user)
})
.then((user) => {
res.send({ user: user })
})
.catch((err) => {
if (err.name === 'ValidationError') {
return res.status(400).send(err)
}
res.status(500).send(err)
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment