我在UserController@getProfilePasswordUserController@postProfilePassword中创建了密码路由、视图和方法

At the moment, if I fill out the new_password field, it gets hashed and submitted to the database correctly, then I can login with the new password.

但我需要能够验证new_passwordnew_password_confirm,以确保它们相同,并验证用户的当前密码.

How can I do that?

EDIT: I added $this->validate to the method, but now I keep getting the error The password confirmation confirmation does not match. even though they do match as I am using a simple password. Also I think I need to check against the current password manually as validator won't do it for me.

public function getProfilePassword(Request $request) {
    return view('profile/password', ['user' => Auth::user()]);
}

public function postProfilePassword(Request $request) {
    $user = Auth::user();

    $this->validate($request, [
        'old_password'          => 'required',
        'password'              => 'required|min:4',
        'password_confirmation' => 'required|confirmed'
    ]);

    $user->password = Hash::make(Input::get('new_password'));
    $user->save();
}

And this is the view

<form action="{{ route('profile/updatepassword') }}" method="post" enctype="multipart/form-data">
    <div class="form-group">
          <label for="name">Current Password</label>
          <input type="password" name="old_password" class="form-control" id="old_password">
    </div>
    <div class="form-group">
          <label for="name">Password</label>
          <input type="password" name="password" class="form-control" id="password">
    </div>
    <div class="form-group">
          <label for="name">New Password</label>
          <input type="password" name="password_confirmation" class="form-control" id="password_confirmation">
    </div>
    <button type="submit" class="btn btn-primary">Change Password</button>
    <input type="hidden" value="{{ Session::token() }}" name="_token">
 </form>

推荐答案

There's a Hash::check() function which allows you to check whether the old password entered by user is correct or not.

usage

if (Hash::check("param1", "param2")) {
 //add logic here
}

param1 - user password that has been entered on the form
param2 - old password hash stored in database

it will return true if old password has been entered correctly and you can add your logic accordingly

for new_password and new_confirm_password to be same, you can add your validation in form request like

'new_password' => 'required',
'new_confirm_password' => 'required|same:new_password'

Laravel相关问答推荐

如何正确使用 Spatie\Laravel Data 来获取数据?

根据路径的开头重定向

前端和管理员分开时未加载laravel 9中间件

覆盖 HTTP 标头的默认设置 (X-FRAME-OPTIONS)

Laravel:转义LIKE子句?

在 laravel 5 中的表单请求验证后传递旧输入

Laravel 5.1 如何在Blade 文件上使用 {{ old('') }} 助手进行无线电输入

.gitignore 不忽略文件夹

在 laravel 如何将额外的数据传递给 mutators 和 accessors

Laravel 5.3 - 将多个文件附加到 Mailables

如何判断是否在laravel中设置了cookie?

laravel 5.4 指定的key太长,为什么数字191

WhereHas Laravel 中的关系计数条件是什么

Laravel 迁移 - 删除列

在 Laravel 测试用例中模拟一个 http 请求并解析路由参数

将参数传递给 Laravel 中的中间件

判断行是否存在,Laravel

Laravel 4 - 文件上传

如何在 Laravel 5.3 中获取当前的语言环境

Laravel 如何在 Eloquent 模型中添加自定义函数?