在学习Sailsjs时,我正在通过一个示例聊天应用程序代码。但是,在成功登录或注册MainController中的函数之后,这些功能似乎是通过res.send(用户)行将在db中找到的或在db中创建的整个用户对象发送到客户端。
我说对了吗?发送密码不是错误和不安全的吗?
还是没发出去?如果是这样的话,是怎么做的?
login action from the api/controllers/MainController.js:
login: function (req, res) {
var username = req.param('username');
var password = req.param('password');
// Users.findByUsername(username)...
// In v0.9.0 the find method returns an empty array when no results are found
// when only one result is needed use findOne.
Users.findOneByUsername(username)
.done(function loginfindUser(err, usr){
if (err) {
// We set an error header here,
// which we access in the views an display in the alert call.
res.set('error', 'DB Error');
// The error object sent below is converted to JSON
res.send(500, { error: "DB Error" });
} else {
if (usr) {
var hasher = require("password-hash");
if (hasher.verify(password, usr.password)) {
req.session.user = usr;
res.send(usr);
} else {
// Set the error header
res.set('error', 'Wrong Password');
res.send(400, { error: "Wrong Password" });
}
} else {
res.set('error', 'User not Found');
res.send(404, { error: "User not Found"});
}
}
});
},发布于 2014-12-05 18:26:16
在用户模型的属性中添加删除密码的toJSON函数
module.exports = {
attributes: {
...
toJSON: function() {
user = this.toObject()
delete user.password
return user
}
}
}如果您对用户的密码进行了正确的加密,将其发送到客户端并不可怕,但仍然被认为是一种糟糕的做法。
https://stackoverflow.com/questions/26921320
复制相似问题