[英]Sending the User model's password to the client side in Sailsjs App
在學習Sailsjs時,我將通過示例聊天應用程序代碼 。 但是,似乎成功登錄或注冊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"});
}
}
});
},
在用戶模型的屬性中添加toJSON
函數以刪除密碼
module.exports = {
attributes: {
...
toJSON: function() {
user = this.toObject()
delete user.password
return user
}
}
}
如果您正確加密了用戶的密碼,將其發送給客戶端並不可怕 ,但這仍然是一種不良做法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.