如何为特定用户实施旧密码和新密码?

Posted

技术标签:

【中文标题】如何为特定用户实施旧密码和新密码?【英文标题】:How to implement old and new password for particular user? 【发布时间】:2021-06-04 17:54:45 【问题描述】:

对于需要将旧密码更改为特定用户的新密码的情况,我有点困惑。为此,我通过他们的用户 ID 获取每个用户,并在那里应用了对 onkeyup 的检查。在这一部分中,每当任何用户按下任何键以生成与旧密码相关的新密码时,我都会在 span 标签中打印消息。但问题是,当我提交更改时,表单也提交了不匹配的密码。

<script>
$('#opassword, #confirm_password').on('keyup', function () 
        if ($('#opassword').val() == $('#confirm_password').val()) 
            $('#error-message').html('Password Matching').css('color', 'green');
         else 
            $('#error-message').html('Password Not Matching').css('color', 'red');
                   
    );

function updatepassword()
        var userid = document.getElementById('sp_customerid').value;
        var oldpassword = document.getElementById('opassword').value;
        var password = document.getElementById('confirm_password').value;
        if (oldpassword == password) 
            alert("Password matched")
         else 
            alert("Password do not matched! Please try again later")
        
        var url = "../api/resetpassword";
        $.post(url, 
            userid : userid,
            password : password,
        , function(data, status) 
            if (data.status == "OK") 
                if (data.statusCode == 1) 
                    $('#updatepasswordmodal').modal('hide');
                 else 
                    var error = data.responseMessage;
                    swal(error, "", "error");
                
             else 
                var error = data.responseMessage;
                swal(error, "", "error");
            
        );
    
</script>

【问题讨论】:

【参考方案1】:

您应该将$.post() 放在updatepassword() 代码的允许分支中。 (我想oldpassword == password 是应该去的分支。)

$('#opassword, #confirm_password').on('keyup', function() 
  if ($('#opassword').val() == $('#confirm_password').val()) 
    $('#error-message').html('Password Matching').css('color', 'green');
   else 
    $('#error-message').html('Password Not Matching').css('color', 'red');
  
);

function updatepassword() 
  var userid = document.getElementById('sp_customerid').value;
  var oldpassword = document.getElementById('opassword').value;
  var password = document.getElementById('confirm_password').value;
  if (oldpassword == password) 
    alert("Password matched")
    var url = "../api/resetpassword";
    $.post(url, 
      userid: userid,
      password: password,
    , function(data, status) 
      if (data.status == "OK") 
        if (data.statusCode == 1) 
          $('#updatepasswordmodal').modal('hide');
         else 
          var error = data.responseMessage;
          swal(error, "", "error");
        
       else 
        var error = data.responseMessage;
        swal(error, "", "error");
      
    );
   else 
    alert("Password do not matched! Please try again later")
  

建议

将您的函数分解成更小的部分:如果它们只有几行长,并且“职责”很少(如果一个函数只做一件事情,而不是更多,最好) :

function getUpdatePasswordData() 
  var oldpassword = document.getElementById('opassword').value;
  var password = document.getElementById('confirm_password').value;
  var userid = document.getElementById('sp_customerid').value;
  return 
    userid,
    password,
    oldpassword
  


function comparePasswords() 
  const  oldpassword, password  = getUpdatePasswordData()
  return oldpassword == password


$('#opassword, #confirm_password').on('keyup', function() 
  if (comparePasswords()) 
    $('#error-message').html('Password Matching').css('color', 'green');
   else 
    $('#error-message').html('Password Not Matching').css('color', 'red');
  
);

function postNewPassword(userid, password) 
  var url = "../api/resetpassword";
  $.post(url, 
    userid,
    password,
  , function(data, status) 
    if (data.status == "OK") 
      if (data.statusCode == 1) 
        $('#updatepasswordmodal').modal('hide');
       else 
        var error = data.responseMessage;
        swal(error, "", "error");
      
     else 
      var error = data.responseMessage;
      swal(error, "", "error");
    
  );


function updatepassword() 
  const  userid, password  = getUpdatePasswordData()
  if (comparePasswords()) 
    alert("Password matched")
    postNewPassword(userid, password)
   else 
    alert("Password do not matched! Please try again later")
  

【讨论】:

以上是关于如何为特定用户实施旧密码和新密码?的主要内容,如果未能解决你的问题,请参考以下文章

通过覆盖旧密码插入新密码

如何在Wordpress中更新新密码之前从db验证旧密码

检查用户密码并创建新密码

Membership ChangePassword(散列)将当前密码与旧密码字段匹配

如何验证firebase用户当前密码

mysql如何修改root用户的密码