Laravel 5.8 change password functionality

一曲冷凌霜 提交于 2020-06-28 04:54:59

问题


I'm currently trying to make change password functionality to my user profile all my inputs are submitted to the controller, but I think there might be something wrong with my function logic maybe?

Tried dumping request on function and dump was successfully returned. But when wrapping a validation variable around a validation process, the dump was not returned. The request redirects back to the profile page with form data.

Controller

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;
}

View

<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>

The result should return 422/error message at least into the console when 'Current password' is wrong, not just redirect back to view and when the password is correct then return 200/success message (not implemented yet.) to console or view.


回答1:


try this

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;
    }



回答2:


You are validating request fields old_password, new_password and password_confirm here:

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

however your are using request fields current and password to verify current password and set a new one:

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

Validated fields and used fields should be the same.




回答3:


Laravel 5.8

Include this function in a controller:

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.'
    ]);
}

Don't forget to send new_password_confirmation as a parameter too, because when we use the validation rule confirmed for new_password for example, Laravel automatically looks for a parameter called new_password_confirmation in order to compare both fields.



来源:https://stackoverflow.com/questions/56305196/laravel-5-8-change-password-functionality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!