2016-07-16 6 views
2

ここでは、ユーザーモジュールで携帯番号を確認しようとしています。トークンを作成してユーザーに送信しましたが、ユーザーがその特定のトークンを使用して検証しようとするたびに、「パスワード」と「塩」が自動的に変更されました。これを避ける方法は?いくつかのいずれかが私を助けて...ここで私が唯一なぜ、IMAN STACKにパスワードと塩が自動的に表示されるのですか?

user.Mobileverification = 'verfied'; 
user.Mobileverificationcode = undefined; 
user.mobileVerificationExpires = undefined; 

3上記の変数を更新したい変わってしまったが、パスワードと塩が変更された理由を私は知らないのですか?

私は下の私のルートを与えている:

app.route('/auth/mobilereset/:token').get(users.mobileresetResetToken); 
app.route('/auth/mobilereset/:token').post(users.mobilereset); 

コントローラ:

exports.mobileresetResetToken = function(req, res) { 
    User.findOne({ 
     Mobileverificationcode :req.params.token, 
     mobileVerificationExpires: { 
      $gt: Date.now() 
     } 
     // resetPasswordToken: req.params.token, 
     // resetPasswordExpires: { 
      // $gt: Date.now() 
     // } 
    }, function(err, user) { 
     if (!user) { 
      res.send({ 
       message: 'Invalid token' 
      }); 


     } else { 

      console.log('working fine'); 
     } 
    }); 
}; 



exports.mobilereset = function(req, res, next) { 


    async.waterfall([ 

     function(done) { 
      User.findOne({ 
       Mobileverificationcode: req.params.token, 
       mobileVerificationExpires: { 
        $gt: Date.now() 
       } 
      }, function(err, user) { 
       if (!err && user) { 

         user.Mobileverification = 'verfied'; 
         user.Mobileverificationcode = undefined; 
         user.mobileVerificationExpires = undefined; 

         user.save(function(err) { 
          if (err) { 
           return res.status(400).send({ 
            message: errorHandler.getErrorMessage(err) 
           }); 
          } else { 
           req.login(user, function(err) { 
            if (err) { 
             res.status(400).send(err); 
            } else { 
             // Return authenticated user 
             res.json(user); 

             done(err, user); 
            } 
           }); 
          } 
         }); 

       } else { 
        return res.status(400).send({ 
         message: 'reset token is invalid or has expired.' 
        }); 
       } 
      }); 
     }, 

    ], function(err) { 
     if (err) return next(err); 
    }); 
}; 

モデル:

var UserSchema = new Schema({ 

    username: { 
     type: String, 
     unique: 'testing error message', 
     required: 'Please fill in a username', 
     trim: true 
    }, 
    password: { 
     type: String, 
     default: '', 
     // validate: [validateLocalStrategyPassword, 'Password should be longer'] 
    }, 
    email: { 
     type: String, 
     trim: true, 
     default: '', 
     // validate: [validateLocalStrategyProperty, 'Please fill in your email'], 
     // match: [/.+\@.+\..+/, 'Please fill a valid email address'] 
    }, 
    Mobilenumber: { 
     type: String, 
     default: '' 
    }, 


    roles: { 
     type: [{ 
      type: String, 
      enum: ['user', 'admin'] 
     }], 
     default: ['user'] 
    }, 
    salt: { 
     type: String 
    }, 
    provider: { 
     type: String, 
     required: 'Provider is required' 
    }, 
    providerData: {}, 
    additionalProvidersData: {}, 

    updated: { 
     type: Date 
    }, 
    created: { 
     type: Date, 
     default: Date.now 
    }, 
    /* For reset password */ 
    Mobileverificationcode: { 
     type: String, 
    }, 
    mobileVerificationExpires: { 
     type: Date 
    }, 
    Mobileverification: { 
     type: String, 
     trim: true, 
     default: 'Not Verified', 
    }, 
    resetPasswordToken: { 
     type: String 
    }, 
    resetPasswordExpires: { 
     type: Date 
    } 
}); 

答えて

0

私はあなたがこれを削除するかどう知っているが、中にはありませんMEAN .jsユーザーモデルの場合、次のコードブロックには注意が必要です。

/** 
* Hook a pre save method to hash the password 
*/ 
UserSchema.pre('save', function (next) { 
    if (this.password && this.isModified('password')) { 
    this.salt = crypto.randomBytes(16).toString('base64'); 
    this.password = this.hashPassword(this.password); 
    } 

    next(); 
}); 

ユーザーデータを保存する直前に呼び出されるものです。それはおそらくパスワードと塩が変化し続ける理由です...あなたはmobile.reset()のuser.saveを呼び出していて、上記のコードブロックはまだどこかに存在しています。

更新: それを行うための可能な方法は次のとおりです。

/** 
* Hook a pre save method to hash the password 
*/ 
UserSchema.pre('save', function (next) { 
    if(!this.isModified('Mobileverification') && !this.isModified('Mobileverificationcode') && !this.isModified('mobileVerificationExpires')) { 
     if (this.password && this.isModified('password')) { 
     this.salt = crypto.randomBytes(16).toString('base64'); 
     this.password = this.hashPassword(this.password); 
     } 
    } 

    next(); 
}); 

それは、このようなニーズに応じて、パスワード変更やモバイルをテストし、この前のセーブフックを改善するなど、いくつかの調整を必要とするかもしれませんが何も壊れていないかどうかを確認する検証。

+0

塩分とパスワードを変更することはできませんどのように変更することができますかMobileverificationcode、mobileVerificationExpires、Mobileverification –

関連する問題