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/成功消息(尚未实现。)到控制台或查看。


慕标5832272
浏览 174回答 3
3回答

qq_花开花谢_0

试试这个public function updatePassword(Request $request){&nbsp; &nbsp; &nbsp; &nbsp; if (!(Hash::check($request->get('old_password'), Auth::user()->password))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // The passwords not matches&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //return redirect()->back()->with("error","Your current password does not matches with the password you provided. Please try again.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return response()->json(['errors' => ['current'=> ['Current password does not match']]], 422);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //uncomment this if you need to validate that the new password is same as old one&nbsp; &nbsp; &nbsp; &nbsp; if(strcmp($request->get('old_password'), $request->get('new_password')) == 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Current password and new password are same&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //return redirect()->back()->with("error","New Password cannot be same as your current password. Please choose a different password.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return response()->json(['errors' => ['current'=> ['New Password cannot be same as your current password']]], 422);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $validatedData = $request->validate([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'old_password' => 'required',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'new_password' => 'required|string|min:6|confirmed',&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; //Change Password&nbsp; &nbsp; &nbsp; &nbsp; $user = Auth::user();&nbsp; &nbsp; &nbsp; &nbsp; $user->password = Hash::make($request->get('new_password'));&nbsp; &nbsp; &nbsp; &nbsp; $user->save();&nbsp; &nbsp; &nbsp; &nbsp; return $user;&nbsp; &nbsp; }

江户川乱折腾

Laravel 5.8将此函数包含在控制器中:public function updatePassword(Request $request){&nbsp; &nbsp; $request->validate([&nbsp; &nbsp; &nbsp; &nbsp; 'password' => 'required',&nbsp; &nbsp; &nbsp; &nbsp; 'new_password' => 'required|string|confirmed|min:6|different:password'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; ]);&nbsp; &nbsp; if (Hash::check($request->password, Auth::user()->password) == false)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return response(['message' => 'Unauthorized'], 401);&nbsp;&nbsp;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; $user = Auth::user();&nbsp; &nbsp; $user->password = Hash::make($request->new_password);&nbsp; &nbsp; $user->save();&nbsp; &nbsp; return response([&nbsp; &nbsp; &nbsp; &nbsp; 'message' => 'Your password has been updated successfully.'&nbsp; &nbsp; ]);}不要忘记发送new_password_confirmation作为参数太多,因为当我们使用验证规则confirmed的new_password,例如,Laravel自动查找名为参数new_password_confirmation,以比较这两个领域。
打开App,查看更多内容
随时随地看视频慕课网APP