[英]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');
}
这里有两个主要问题。
一方面, $userpass
返回null,因为get()
不是影响列的适当函数。 您可以为此使用pluck(请参阅查询构建器docs )
无论如何,只要像这样获取用户,就可以调用属性:
$userpass = User::find(Session::get('userid'))->password;
您正在尝试将哈希密码与普通密码进行比较。 Laravel默认使用Guard来管理用户身份验证,Guard使用Hash::make
来存储它。 您应该将散列与以下内容进行比较:
Hash::check($oldpass, $userpass)
您也可以只使用Guard来检查用户凭据是否正确,方法是使用Auth::validate($credentials)
(请参阅Laravel Security ),然后更改密码,例如:
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');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.