Gay*_*ewa 5 node.js sails.js waterline
当使用水线ORM时,如果我想使用默认发送的bluebird promise api如何将处理传递回控制器.
以下是代码:
module.exports = {
//Authenticate
auth: function (req, res) {
user = req.allParams();
//Authenticate
User.authenticate(user, function (response) {
console.log(response);
if (response == true) {
res.send('Authenticated');
} else {
res.send('Failed');
}
});
}
};
module.exports = {
// Attributes
// Authenticate a user
authenticate: function (req, cb) {
User.findOne({
username: req.username
})
.then(function (user) {
var bcrypt = require('bcrypt');
// check for the password
bcrypt.compare(req.password, user.password, function (err, res) {
console.log(res);
if (res == true) {
cb(true);
} else {
cb(false);
}
});
})
.catch(function (e) {
console.log(e);
});
}
};
Run Code Online (Sandbox Code Playgroud)
我只是想尝试实现身份验证功能.业务逻辑很直接.我感到困惑的是,请求流是如何从那时传回控制器的.如果我尝试返回响应,则承诺不响应,但执行cb(value)有效.
与承诺合作的关键是永远不要打破链条.承诺链取决于返回 promise或值或抛出错误的每一步.
以下是您的代码的重写.注意
.auth();它可能在某些时候有用).promisifyAll()来制作bcrypt游戏.authenticate()通过明确地使用username和password参数,我与您的请求/响应基础结构分离.这样它就可以更容易地重复使用.所以现在我们有(不是100%测试,我没有打扰安装水线):
module.exports = {
// authenticate the login request
auth: function (req, res) {
var params = req.allParams();
return User.authenticate(params.username, params.password)
.then(function () {
res.send('Authenticated');
})
.fail(function (reason) {
res.send('Failed (' + reason + ')');
});
}
};
Run Code Online (Sandbox Code Playgroud)
和
var Promise = require("bluebird");
var bcrypt = Promise.promisifyAll(require('bcrypt'));
module.exports = {
// check a username/password combination
authenticate: function (username, password) {
return User.findOne({
username: username
})
.then(function (user) {
return bcrypt.compareAsync(password, user.password)
})
.catch(function (err) {
// catch any exception problem up to this point
console.log("Serious problem during authentication", err);
return false;
})
.then(function (result) {
// turn `false` into an actual error and
// send a less revealing error message to the client
if (result === true) {
return true;
} else {
throw new Error("username or password do not match");
}
});
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4133 次 |
| 最近记录: |