Skip to content

Commit

Permalink
Code improved
Browse files Browse the repository at this point in the history
  • Loading branch information
Hemil96 committed Aug 13, 2018
1 parent c4db724 commit 0b5bb2c
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 34 deletions.
3 changes: 1 addition & 2 deletions controller/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const authenticate = (req, res) => {
.then((user) => {
this.user = user;
if (!user) return res.status(403).send({ success: false, msg: 'Authentication failed, User not found' });
return utils.comparePassword(req.body.password, user.password);
return utils.comparePassword(String(req.body.password), String(user.password));
})
.then((isMatch) => {
if (!isMatch) return res.status(403).send({ success: false, msg: 'Authenticaton failed, wrong password.' });
Expand All @@ -23,7 +23,6 @@ const authenticate = (req, res) => {
});
};


// POST /adduser
const addNew = (req, res) => {
if ((!req.body.name) || (!req.body.password)) {
Expand Down
2 changes: 1 addition & 1 deletion controller/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const postClients = (req, res) => {
// GET /getClients
const getClients = (req, res) => {
// Use the Client model to find all clients
Client.find({ userId: req.query.userId })
Client.find({ userId: req.params.id })
.then((foundClient) => {
if (!foundClient) return res.status(404).json({ error: 'Client not found' });
return res.json({ message: 'Client found', data: foundClient });
Expand Down
30 changes: 0 additions & 30 deletions model/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,4 @@ userSchema.pre('save', function (next) {
});


userSchema.pre('save', function (next) {
var user = this;
if (this.isModified('password') || this.isNew) {
bcrypt.genSalt(10, (err, salt) => {
if (err) {
return next(err);
}
bcrypt.hash(user.password, salt, (err, hash) => {
if (err) {
return next(err);
}
user.password = hash;
next();
});
});
} else {
return next();
}
});


userSchema.methods.comparePassword = function (passw, cb) {
bcrypt.compare(passw, this.password, (err, isMatch) => {
if (err) {
return cb(err);
}
cb(null, isMatch);
});
};

module.exports = mongoose.model('User', userSchema);
2 changes: 1 addition & 1 deletion routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ router.get('/getinfo', actions.getinfo); // Retrive All Users

// Client routes
router.post('/clients', auth.isAuthenticated, client.postClients); // Create New User
router.get('/clients', auth.isAuthenticated, client.getClients); // Authenticate client
router.get('/client/:id', auth.isAuthenticated, client.getClients); // Authenticate client

// Oauth routes
router.get('/oauth2/authorize', oauth2.authorization); // Ask for permission
Expand Down

0 comments on commit 0b5bb2c

Please sign in to comment.