2016-06-01 49 views
8

Tôi đã tạo ra con đường mật khẩu, xem và phương pháp trong [email protected][email protected]Làm cách nào để xác nhận xác nhận mật khẩu mới, mới và mới trong Laravel 5?

Tại thời điểm này, nếu tôi điền thông tin vào lĩnh vực new_password, nó được băm và nộp cho cơ sở dữ liệu một cách chính xác, sau đó tôi có thể đăng nhập với mật khẩu mới .

Nhưng tôi cần có khả năng xác thực new_passwordnew_password_confirm để đảm bảo chúng giống nhau và xác thực mật khẩu hiện tại của người dùng.

Tôi có thể làm như thế nào?

EDIT: Tôi đã thêm $this->validate vào phương pháp, nhưng bây giờ tôi tiếp tục gặp lỗi The password confirmation confirmation does not match. mặc dù chúng khớp với nhau khi tôi đang sử dụng mật khẩu đơn giản. Ngoài ra tôi nghĩ rằng tôi cần phải kiểm tra mật khẩu hiện tại theo cách thủ công như validator sẽ không làm điều đó cho tôi.

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

Và đây là quan điểm

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

Trả lời

29

Có một chức năng Hash::check() cho phép bạn kiểm tra xem mật khẩu cũ nhập vào bởi người sử dụng là đúng hay không.

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 

nó sẽ trả về true nếu mật khẩu cũ đã được nhập một cách chính xác và bạn có thể thêm logic của bạn cho phù

cho new_passwordnew_confirm_password là cùng, bạn có thể thêm xác nhận của bạn trong yêu cầu hình thức như

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

Cảm ơn bạn cho những lời khuyên hữu ích. Tôi đã sử dụng 'xác nhận' thay vì' cùng: new_password' - chỉ cần thay đổi nó và nó đã làm việc. Và tôi sẽ sử dụng 'Hash :: check()' để xác thực mật khẩu hiện tại. – Halnex

+0

vui mừng vì nó đã giúp :) – Sid

+6

Một mẹo hay khác để sử dụng trong trường xác nhận: quy tắc xác thực 'đã xác nhận' <[link] (https://laravel.com/docs/5.4/validation#rule-confirmed)>. Quy tắc thực tế của bạn sẽ chỉ là một dòng, như sau: '' new_password '=>' required | confirmed''. Và tên trường xác nhận phải là 'new_password_confirmation'. – Corner

3

Bạn có thể thêm confirmed vì nó để xác nhận ol d mật khẩu. Và 'required|confirmed' bạn thay đổi để 'required|same:password' để so sánh passwordpassword confirmation

'old_password' => 'required|confirmed', 'password' => 'required|min:4', 'password_confirmation' => 'required|same:password'

Chúc may mắn!

+2

Xác nhận đã được xác nhận phải có trường phù hợp với foo_confirmation. Ví dụ: nếu trường dưới xác thực là mật khẩu, trường khớp password_confirmation phải có mặt trong đầu vào. –

+0

Điều này sai theo tài liệu: https://laravel.com/docs/5.1/validation#rule-confirmed – Jonjie

0

Bạn có thể thực hiện việc này bằng cách tạo quy tắc xác thực tùy chỉnh (ví dụ: tôi đang sử dụng current_passwordnew_password làm tên đầu vào).

Đặt này trong AppServiceProvider::boot():

Validator::extend('current_password', function ($attribute, $value, $parameters, $validator) { 
    $user = User::find($parameters[0]); 

    return $user && Hash::check($value, $user->password); 
}); 

Bây giờ bạn có thể sử dụng sau đây trong điều khiển của bạn:

$user = auth()->user(); // or pass an actual user here 

$this->validate($request, [ 
    'current_password' => 'required_with:new_password|current_password,'.$user->id, 
]); 
Các vấn đề liên quan