【问题标题】:Trying to store hashed username in database尝试将散列用户名存储在数据库中
【发布时间】:2016-04-19 19:23:23
【问题描述】:

我正在尝试使用 JavaScript-MD5 插件对用户名进行哈希处理,然后将其存储到数据库中以与 Jdenticon 一起使用。我可以使用var hash = md5($scope.username); 对密码进行哈希处理并将其记录到控制台,但无法将其传递给我的 newUser 变量。

  1. 注册控制器

    $scope.register = function(){
    var hash = md5($scope.username);
    console.log(hash);
    var newUser = {
      email: $scope.email,
      password: $scope.password,
      username: $scope.username,
      userHash: hash
    };
    
    $http.post('/users/register', newUser).then(function(){
      $scope.email = '';
      $scope.password = '';
      $scope.username = '';
      userHash = '';
    };
    
  2. 注册路线:

    app.post('/users/register', function(req, res) {
    
      bcrypt.genSalt(10, function(err, salt) {
        bcrypt.hash(req.body.password, salt, function(err, hash) {
          var user = new User({
            email: req.body.email,
            password: hash,
            username: req.body.username,
            userHash: req.body.userHash
          });
          console.log(user);
    
          user.save(function(err) {
             if (err) return res.send(err);
    
             return res.send();
          });
        });
      });
    });
    

【问题讨论】:

  • 您的客户端控制台中是否有任何错误?您的节点控制台记录什么?
  • req.body.userHash 在服务器端显示什么值?
  • 没有显示 userHash 的值。登录测试用户:{ created: Tue Apr 19 2016 15:37:25 GMT-0400 (Eastern Daylight Time), _id: 57168933dbe10f0c2860c180, username: 'green', password: '$2a$10$U4pqSpgjM74pqSpgjM7NwEj8BnR4I.uWlODNXyld2NHmj0iGBgcETawCCZkk0G', email: 'green@blue.com' }
  • userHash 存在于您的User 模型中吗?
  • 不是,感谢您的帮助。

标签: javascript angularjs mongodb hash md5


【解决方案1】:

我猜你可能错过了User 模型中的userHash 属性,这就是为什么你不能在数据库中存储userHash

所以你应该首先在你的User 模型中包含userHash 然后应该可以正常工作。

喜欢:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var UserSchema= new Schema({
    username : {
        type: String,
        required: true
    },
    email: {
        type: String
    },
    password: {
        type: String
    },
    userHash:{
        type: String
    }
});
mongoose.model('User', UserSchema);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-23
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    相关资源
    最近更新 更多