【问题标题】:Return user friend without authentification informations返回没有认证信息的用户好友
【发布时间】:2015-06-28 09:00:44
【问题描述】:

我正在尝试构建一个具有一些社交功能的 nodeJS 应用程序。 每个用户都使用登录名/密码进行身份验证,服务器返回一个身份验证令牌。

每个用户都可以有朋友。这是我的用户模型:

var UserSchema = mongoose.Schema({
password: {
    type: String,
    required: true
},
email: {
    type: String,
    unique: true,
    required: true
},
name: String,
tokken: String,
tokkenValidUntil: Date,
friends: [{
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User"
    }
}],

});

在某些时候,我必须发送用户的朋友列表。 我正在使用

return done(null, user);

对用户进行序列化,与用户交友。

问题:用户好友序列化返回时,包含token,存在安全问题。

在发回之前,我是否必须删除每个用户的令牌?

例如:

user.friends.forEach(function(friendUser) {
     friend.user.token = "";
});

我在用户和朋友类中做错了吗?

感谢您的帮助!

【问题讨论】:

标签: node.js


【解决方案1】:

您可以尝试仅使用指定的字段进行存档。

Mongoose - Model

例如:

// name LIKE john and only selecting the "name" and "friends" fields, executing immediately
MyModel.find({ name: /john/i }, 'name friends', function (err, docs) { })

【讨论】:

    猜你喜欢
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多