如何在拉拉维尔中获取加密的密码值


How to get encrypted password value in laravel?

我正在尝试为此更改用户的密码,我需要检查该用户的旧密码是否与我从html"oldpass"文本框中获得的值匹配。如果现有密码值和"oldpass"值匹配,则新密码将在数据库中更新。我从数据库中获得的密码值是加密的。

$userpass = User::where('id', '=', Session::get('

userid'))->get(array('password'));问题是$userpass返回一个 空值 .

这是代码:

    $oldpass = Hash::make(Input::get('oldpass'));//Getting password from html form
    $userpass = User::where('id', '=', Session::get('userid'))->get(array('password'));//Getting password value from database Users table
    if ($oldpass === $userpass) {
         User::where('id', '=', Session::get('userid'))
            ->update(array(
                'password' => Hash::make(Input::get('newpass'))
    ));
    } else {
        Return View::make('changepass.changepass')
                        ->with('errormessage', 'Password does not match');
    }

这里有两个主要问题。

  1. 一方面,$userpass返回 null 是因为get()不是处理列的合适函数。您可以为此使用 pluck(请参阅查询构建器文档)

    无论如何,一旦你获取用户,你就可以调用该属性,如下所示:

    $userpass = User::find(Session::get('userid'))->password;

  2. 您正在尝试将散列密码与纯密码进行比较。拉拉维尔使用后卫默认情况下,管理用户身份验证和防护使用Hash::make来存储它。您应该将哈希与以下内容进行比较:

    Hash::check($oldpass, $userpass)

您也可以使用 Guard 检查用户凭据Auth::validate($credentials)是否正确(请参阅 Laravel 安全性),然后更改密码,如下所示:

if(Auth::validate('id' => Session::get('userid'), 'password' => Input::get('oldpass'))){
  //Assuming user was authenticated before. If not use Auth::attempt instead of validate
  Auth::user()->password = Hash::make(Input::get('newpass')); 
} else {
    Return View::make('changepass.changepass')
                    ->with('errormessage', 'Password does not match');
}