1

嘿,伙计们,这里是我的猫鼬模式,它可以创建一个用户令牌,该令牌将包含在密码重置链接中,我只是想知道它看起来是否正确?此外,不确定如何准确调用新令牌,在创建用户后,User.save 或 User.findAndUpdate 是否会在路由文件中包含令牌工作?另外,我应该在令牌字段中插入什么作为将从此处加密的令牌中提取的值?

这是文件:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    ObjectId = mongoose.Schema.Types.ObjectId,
    bcrypt = require('bcrypt-nodejs'),
    SALT_WORK_FACTOR = 10;

var crypto = require('crypto');
var UserToken;

var UserSchema = new Schema({ 
    email: { type: String, required: true, lowercase:true, index: { unique: true } },
    password: { type: String, required: true },
    firstName: {type: String, required: true},
    lastName: {type: String, required: true},
    phone: {type: Number, required: true},
    birthday: {type: Date, required: true},
    friendRequest: {type: Object},
    notifications: {type: Object},
    friend: {type: Object},
    date_created: {type: Date},
    token: {type: String}
}, {collection: "users"});

UserSchema.statics.new = function(_id, fn) {
    var user = new UserToken();
    crypto.randomBytes(48, function(ex, buff) {
        var token = buff.toString('based64').replace(/\//g, '_').replace(/\+/g, '-');
        user.token = _id + '|' + token.toString().slice(1,24);
        user._id = _id;
        user.save(fn);
    });

};

module.exports = mongoose.model('User', UserSchema);
4

0 回答 0