Laravel 5.1 更改密码功能
Laravel 5.1 change password functionality
我的 laravel 应用程序需要更改密码功能。
我创建了这个视图:
{!! Form::password('old_password', ['class'=>'form-control']) !!}
{!! Form::password('password', ['class'=>'form-control']) !!}
{!! Form::password('password_confirmation', ['class'=>'form-control']) !!}
然后在我的控制器中检查 old_password
输入的值是否与用户的当前密码相同。
if(bcrypt($request->old_password) !=$user->password) return redirect()->back()
问题是上面的条件总是成立的。即使用户输入有效密码,此条件也将 return 为真!
为什么会这样?
顺便说一句,我在我的用户模型中散列密码:
public function setPasswordAttribute($password){
$this->attributes['password'] = bcrypt($password);
}
有一个名为 Auth::validate($credentials)
的有用的身份验证函数,您可以在其中传递 [username, password]
或 [email, password]
组合。这将在不登录用户的情况下检查提供的 $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...
}
我的 laravel 应用程序需要更改密码功能。 我创建了这个视图:
{!! Form::password('old_password', ['class'=>'form-control']) !!}
{!! Form::password('password', ['class'=>'form-control']) !!}
{!! Form::password('password_confirmation', ['class'=>'form-control']) !!}
然后在我的控制器中检查 old_password
输入的值是否与用户的当前密码相同。
if(bcrypt($request->old_password) !=$user->password) return redirect()->back()
问题是上面的条件总是成立的。即使用户输入有效密码,此条件也将 return 为真! 为什么会这样? 顺便说一句,我在我的用户模型中散列密码:
public function setPasswordAttribute($password){
$this->attributes['password'] = bcrypt($password);
}
有一个名为 Auth::validate($credentials)
的有用的身份验证函数,您可以在其中传递 [username, password]
或 [email, password]
组合。这将在不登录用户的情况下检查提供的 $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...
}