【发布时间】:2014-11-14 01:02:11
【问题描述】:
在学习 Sailsjs 时,我正在经历 example Chat application code。但似乎在成功登录或注册后 MainController 中执行这些操作的功能是通过 res.send(user) 行将在 db 中找到或在 db 中创建的整个 User 对象发送到客户端。
我说的对吗?发送密码不是错误和不安全吗?
或者它只是没有发送?如果有,怎么做?
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"});
}
}
});
},
【问题讨论】: