Php Laravel 5.1更改密码功能
我需要更改我的laravel应用程序的密码功能。 我创建了以下视图:Php Laravel 5.1更改密码功能,php,laravel-5.1,bcrypt,change-password,Php,Laravel 5.1,Bcrypt,Change Password,我需要更改我的laravel应用程序的密码功能。 我创建了以下视图: {!! Form::password('old_password', ['class'=>'form-control']) !!} {!! Form::password('password', ['class'=>'form-control']) !!} {!! Form::password('password_confirmation', ['class'=>'form-control']) !!} 然后
{!! Form::password('old_password', ['class'=>'form-control']) !!}
{!! Form::password('password', ['class'=>'form-control']) !!}
{!! Form::password('password_confirmation', ['class'=>'form-control']) !!}
然后在我的控制器中,我检查为旧密码输入的值是否与用户的当前密码相同
if(bcrypt($request->old_password) !=$user->password) return redirect()->back()
问题是上述条件始终为真。此分钟即使用户输入有效密码,此条件也将返回true!
为什么会这样?
顺便说一下,我正在对我的用户模型中的密码进行哈希运算:
public function setPasswordAttribute($password){
$this->attributes['password'] = bcrypt($password);
}
有一个名为Auth::validate($credentials)
的有用身份验证功能,您可以在其中传递[用户名、密码]
或[电子邮件、密码]
组合。这将检查所提供的$credentials
是否有效,而无需用户登录
因此,在您的AuthController
中,您将检查:
...
$credentials = [
'email' => $request->get('email'),
'password' => $request->get('old_password'),
];
if(\Auth::validate($credentials)) {
// TODO: Old password is correct, do your thing
// Change password and login, OR
// Send them to the login page
}
return redirect()->back()->withError('Incorrect old password');
希望这有帮助
干杯 bcrypt()
每次生成一个随机盐。为了检查密码,我应该使用Hash::check()
文档中的示例:
if (Hash::check('plain-text-password', $hashedPassword)) {
// The passwords match...
}
谢谢你的回复。我找到了更好的解决方案。如果要检查密码是否不匹配,请删除密码匹配的代码>。