Last active
December 6, 2016 10:54
-
-
Save cronoh/5cc760c83a94af42e3f0e47f520c947c to your computer and use it in GitHub Desktop.
This file contains 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
User.get({username: req.body.username}, function(err, user) { | |
if(err) {return console.log(err);} | |
if(user) { | |
res.status(404).json({error: ["Username already exist."]}); | |
} else { | |
var user = new User({username: req.body.username, provider: 'local', email: req.body.email, password: bcrypt.hashSync(req.body.password, 10)}); | |
user.put(function(err, success){ | |
if(err) {return console.log(err)} | |
var token = jwt.encode(user, 'lkmaspokjsafpaoske1a8asda0s9a'); | |
res.json({token: token, user: user}); | |
}); | |
} | |
}); |
This file contains 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
User.get({ username: req.body.username }, function(err, user) { | |
if (err) { | |
return res.status(400).json({ error: ["Error when fetching user."] }); | |
} | |
if (user) { | |
return res.status(404).json({ error: ["Username already exist."] }); | |
} | |
var user = new User({ username: req.body.username, provider: 'local', email: req.body.email, password: bcrypt.hashSync(req.body.password, 10) }); | |
user.put(function(err, success){ | |
if (err) { | |
return res.status(500).json({ error: ["Error when creating user."] }); | |
} | |
var token = jwt.encode(user, 'lkmaspokjsafpaoske1a8asda0s9a'); | |
return res.json({ token: token, user: user }); | |
}); | |
}); |
This file contains 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
User | |
.getAsync({ username: req.body.username }) | |
.then(user => { | |
if (user) { | |
return res.status(404).json({ error: ["Username already exist."] }); | |
} | |
const user = new User({ | |
username: req.body.username, | |
provider: 'local', | |
email: req.body.email, | |
password: bcrypt.hashSync(req.body.password, 10) | |
}); | |
return user | |
.putAsync() | |
.then(success => res.json({ token: jwt.encode(user, 'lkmaspokjsafpaoske1a8asda0s9a'), user: user })); | |
}) | |
.catch(err => res.status(500).json({ error: ["There was an error processing your request."] })); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment