为了账号安全,请及时绑定邮箱和手机立即绑定

Laravel 5.8 修改密码功能

Laravel 5.8 修改密码功能

PHP
慕标5832272 2021-06-29 17:43:40
我目前正在尝试为我的用户配置文件更改密码功能,我的所有输入都提交给控制器,但我认为我的功能逻辑可能有问题?尝试对函数进行转储请求并成功返回转储。但是在围绕验证过程包装验证变量时,不会返回转储。请求重定向回带有表单数据的个人资料页面。控制器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 回答

?
qq_花开花谢_0

TA贡献1835条经验 获得超7个赞

试试这个


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;

    }


查看完整回答
反对 回复 2021-07-09
?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

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自动查找名为参数new_password_confirmation,以比较这两个领域。


查看完整回答
反对 回复 2021-07-09
  • 3 回答
  • 0 关注
  • 173 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信