流星密码验证

Meteor password validation

我正在努力弄清楚如何使用我的用户架构进行密码验证,这就是我目前所做的事情:

这是架构:

Schema = {};
Schema.UserProfile = new SimpleSchema({
  //LOTS OF FIELDS WORKING FINE
});

Schema.User = new SimpleSchema({
    username: {
        type: String,
        optional: true
    },
    emails: {
        type: Array,
    },
    "emails.$": {
        type: Object
    },
    "emails.$.address": {
        type: String,
        regEx: SimpleSchema.RegEx.Email
    },
    "emails.$.verified": {
        type: Boolean
    },
    createdAt: {
        type: Date
    },
    profile: {
        type: Schema.UserProfile,
        label: "Perfil",
        optional: false
    },
    services: {
        type: Object,
        blackbox: true
    },
    "services.$": {
        type: Object,
        blackbox: true
    },
    "services.$.password": {   // IS IT RIGHT?
      type: String,
      label: "Senha",
      min: 8
    },
    roles: {
        type: String,
        optional: true
    }
});

Meteor.users.attachSchema(Schema.User);

这就是我的储蓄方式:

let company = {};
company = {
  email: $('#email').val(),
  password: $('#password').val(),
  roles: 'company',
  profile: companyProfile
}

Meteor.call('saveUser', company, function(error, result) {
  if ( error ) {
    console.log(error);    
  }
}

 Meteor.methods({
  saveUser: function(data) {
    return Accounts.createUser(data);
  }
});

我还是个初学者,所以我可能在这里搞砸了。当我尝试创建用户时,如果任何字段存在问题,验证会按预期抛出错误。但是,如果我忘记了密码,不会抛出任何错误,我在这里做错了什么?

----------------------------更新------------ ----------------------

感谢@aedm 的回答。我真的希望我能有这样的东西来验证密码和密码确认:

password: {
    type: String,
    label: "Senha",
      min: 8
  },
  password_confirmation: {
    type: String,
    label: "Confirmação de Senha",
    min: 8,
    custom: function () {
      if (this.value !== this.field('password').value) {
        return "passwordMismatch";
      }
    }
  },

那我想这不可能吧?

Meteor 只存储密码的 bcrypt 散列,它总是长于 8 个字符。

相反,您的 saveUser 方法应该在不满足密码条件时抛出错误。

Meteor.methods({
  saveUser: function(data) {
    if (data.password.length < 8) throw new Meteor.Error("Password too short");
    return Accounts.createUser(data);
  }
});

我会尽量避免为用户定义模式,Meteor 在没有模式的情况下处理得很好,而且如何正确地做到这一点并不简单。