Laravel 5.8更改密码功能

时间:2019-05-25 13:19:45

标签: php laravel laravel-5.8

我目前正在尝试对用户个人资料进行更改密码功能,所有输入均提交给控制器,但是我认为功能逻辑可能有问题吗?

尝试对函数进行转储请求,并且转储已成功返回。但是,当在验证过程中包装验证变量时,不会返回转储。该请求将使用表单数据重定向回到个人资料页面。

控制器

public function updatePassword(Request $request)
{
    $this->validate($request, [
        'old_password' => 'required',
        'new_password' => 'required|confirmed',
        'password_confirm' => 'required'
    ]);

    $user = User::find(Auth::id());

    if (!Hash::check($request->current, $user->password)) {
        return response()->json(['errors' => 
            ['current' => ['Current password does not match']]], 422);
    }

    $user->password = Hash::make($request->password);
    $user->save();

    return $user;
}

查看

<form method="POST" action="{{ route('update-password') }}">
    @csrf
    @method('PUT')
    <div class="form-group row">
        <label for="old_password" class="col-md-2 col-form-label">{{ __('Current password') }}</label>
        <div class="col-md-6">
            <input id="old_password" name="old_password" type="password" class="form-control" required autofocus>
        </div>
    </div>
    <div class="form-group row">
        <label for="new_password" class="col-md-2 col-form-label">{{ __('New password') }}</label>
        <div class="col-md-6">
            <input id="new_password" name="new_password" type="password" class="form-control" required autofocus>
        </div>
    </div>
    <div class="form-group row">
        <label for="password_confirm" class="col-md-2 col-form-label">{{ __('Confirm password') }}</label>

        <div class="col-md-6">
            <input id="password_confirm" name="password_confirm" type="password" class="form-control" required
                   autofocus>
        </div>
    </div>
    <div class="form-group login-row row mb-0">
        <div class="col-md-8 offset-md-2">
            <button type="submit" class="btn btn-primary">
                {{ __('Submit') }}
            </button>
        </div>
    </div>
</form>

当“当前密码”错误时,结果至少应将422 /错误消息返回到控制台,而不仅仅是重定向回查看,并且密码正确后,再将200 /成功消息(尚未实现)返回到控制台。或查看。

3 个答案:

答案 0 :(得分:1)

尝试

public function updatePassword(Request $request){
        if (!(Hash::check($request->get('old_password'), Auth::user()->password))) {
            // The passwords not matches
            //return redirect()->back()->with("error","Your current password does not matches with the password you provided. Please try again.");
            return response()->json(['errors' => ['current'=> ['Current password does not match']]], 422);
        }
        //uncomment this if you need to validate that the new password is same as old one

        if(strcmp($request->get('old_password'), $request->get('new_password')) == 0){
            //Current password and new password are same
            //return redirect()->back()->with("error","New Password cannot be same as your current password. Please choose a different password.");
            return response()->json(['errors' => ['current'=> ['New Password cannot be same as your current password']]], 422);
        }
        $validatedData = $request->validate([
            'old_password' => 'required',
            'new_password' => 'required|string|min:6|confirmed',
        ]);
        //Change Password
        $user = Auth::user();
        $user->password = Hash::make($request->get('new_password'));
        $user->save();
        return $user;
    }

答案 1 :(得分:0)

您正在此处验证请求字段old_passwordnew_passwordpassword_confirm

$this->validate($request, [
    'old_password' => 'required',
    'new_password' => 'required|confirmed',
    'password_confirm' => 'required'
]);

无论您使用的是请求字段currentpassword来验证当前密码并设置一个新密码:

if (!Hash::check($request->current, $user->password)) {
// ...
$user->password = Hash::make($request->password);

验证的字段和使用的字段应该相同。

答案 2 :(得分:0)

Laravel 5.8

在控制器中包含此功能:

public function updatePassword(Request $request)
{
    $request->validate([
        'password' => 'required',
        'new_password' => 'required|string|confirmed|min:6|different:password'          
    ]);

    if (Hash::check($request->password, Auth::user()->password) == false)
    {
        return response(['message' => 'Unauthorized'], 401);  
    } 

    $user = Auth::user();
    $user->password = Hash::make($request->new_password);
    $user->save();

    return response([
        'message' => 'Your password has been updated successfully.'
    ]);
}

也不要忘记发送new_password_confirmation作为参数,因为例如当我们对confirmed使用验证规则new_password时,Laravel会自动寻找一个名为{{1 }},以便比较两个字段。