Last active
April 11, 2017 12:36
-
-
Save mkozhukharenko/a19c341f61795b67390f393af36a50df to your computer and use it in GitHub Desktop.
Expressks auth with promise .js
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 } | |
}) | |
.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