Recent Post

Giới hạn số lần đăng nhập lỗi Laravel với ThrottlesLogins

Vào vendor/laravel/framework/src/Illuminate/Foundation/Auth/ThrottlesLogins.php ở function
protected function hasTooManyLoginAttempts(Request $request)
{
     return $this->limiter()->tooManyAttempts($this->throttleKey($request),5, 1);
}
 Có 5 là số lần đăng nhập lỗi, 1 là số phút khóa đăng nhập.
Vào LoginController phần xử lí thêm code sau vào đầu function
if ($this->hasTooManyLoginAttempts($request)) {
      $this->fireLockoutEvent($request);
      $seconds = $this->limiter()->availableIn($this->throttleKey($request));
      $messages = new MessageBag(['errorlogin'=>'Bị cấm đăng nhập trong '. $seconds.' giây']);
      return redirect()->back()->withErrors($messages); 
}
Thêm code sau vào phần login lỗi
$this->incrementLoginAttempts($request);
$times = 5-Cache::get($this->throttleKey($request));
$messages = new MessageBag(['errorlogin'=>'Email, username hoặc mật khẩu không đúng. Còn '.$times.' lần thử trước khi bị cấm']);
return redirect()->back()->withErrors($messages);

0 Nhận xét