Laravel 检查旧密码,更改新密码时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50074815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:41:09  来源:igfitidea点击:

Laravel check for old password, when change new password

phplaravel

提问by Jadasdas

I want check if user pass new_password input as current password, I want to redirect with message: Your current password can't be with new password. How I can check this? I want do system change password user, but I want denied old passwords pass. How I can do ?

我想检查用户是否通过 new_password 输入作为当前密码,我想用消息重定向:Your current password can't be with new password。我怎么能检查这个?我想做系统更改密码用户,但我想拒绝旧密码通过。我该怎么办?

if (!(Hash::check($request->old_password, Auth::user()->password))) {
    return response()->json(['errors' => ['Your current password can't be with new password']], 400);
}

Code is not working. Do I need write old passwords to database?

代码不起作用。我需要将旧密码写入数据库吗?

回答by Mahesh Bhattarai

use Illuminate\Support\Facades\Hash;
$user = User::findOrFail($id);

/*
* Validate all input fields
*/
$this->validate($request, [
    'password' => 'required',
    'new_password' => 'confirmed|max:8|different:password',
]);

if (Hash::check($request->password, $user->password)) { 
   $user->fill([
    'password' => Hash::make($request->new_password)
    ])->save();

   $request->session()->flash('success', 'Password changed');
    return redirect()->route('your.route');

} else {
    $request->session()->flash('error', 'Password does not match');
    return redirect()->route('your.route');
}

回答by Jonson

$validator = Validator::make($request->all(), [
    'old_password' => [
        'required', function ($attribute, $value, $fail) {
            if (!Hash::check($value, Auth::user()->password)) {
                $fail('Old Password didn\'t match');
            }
        },
    ],
]);

if($validator->fails()) {
    return redirect()->back()->withInput()->withErrors($validator);
}

You may need to include the following libraries in your controller.

您可能需要在控制器中包含以下库。

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Hash;

回答by Nilaksha Perera

Check your old password field against the session password and return the error followed by the required validation.

根据会话密码检查旧密码字段并返回错误,然后进行所需的验证。

if (!Hash::check($request['old_password'], Auth::user()->password)) {
      return response()->json(['error' => ['The old password does not match our records.'] ]);
 }

You also need to include the following libraries in your controller.

您还需要在控制器中包含以下库。

use Auth;
use Illuminate\Support\Facades\Hash;

回答by Priya

You can do like this

你可以这样做

if ( Hash::make($request->new_password) == Auth::user()->password) {
        return response()->json(['errors' => ['Your current password can't be with new password']], 400);
    }