Laravel : Increase time on second time login attempts

  • A+
Category:Languages

Currently five login attempts blocks user for 1 minute and its working fine with the following code :

if ($this->hasTooManyLoginAttempts($request)) {     $this->fireLockoutEvent($request);     return $this->sendLockoutResponse($request); } 

What i want is that, When a user gets unblocked again after the first attempts, On the second attempts the block time should increase to 3 minutes.

I searched around, But could not found anything, Is there any way around it ?

 


I would suggest you try the following code. Please ask if anything is unclear.

$minutes = 3; $key = $this->throttleKey($request); $rateLimiter = $this->limiter();  if ($this->hasTooManyLoginAttempts($request)) {      $attempts = $rateLimiter->attempts($key);      if ($attempts > 1) {         $attempts === 2 && $rateLimiter->clear($key);         $this->decayMinutes = ($attempts - 1) * $minutes;         $attempts === 2 && $this->incrementLoginAttempts($request);         $this->incrementLoginAttempts($request);     }      $this->fireLockoutEvent($request);     return $this->sendLockoutResponse($request); } 

Code for incremental blocking:

$minutes = 3; $key = $this->throttleKey($request); $rateLimiter = $this->limiter();  if ($this->hasTooManyLoginAttempts($request)) {      $attempts = $rateLimiter->attempts($key);     $rateLimiter->clear($key);     $this->decayMinutes = $attempts === 1 ? 1 : ($attempts - 1) * $minutes;      for ($i = 0; $i < $attempts; $i++) {         $this->incrementLoginAttempts($request);     }      $this->fireLockoutEvent($request);     return $this->sendLockoutResponse($request); } 

Code for incremental blocking with cache:

$minutes = 3; $key = $this->throttleKey($request); $rateLimiter = $this->limiter();  if ($this->hasTooManyLoginAttempts($request)) {      $attempts = $rateLimiter->attempts($key);     $rateLimiter->clear($key);      $reflection = new /ReflectionClass($rateLimiter);     $property = $reflection->getProperty('cache');     $property->setAccessible(true);     $cache = $property->getValue($rateLimiter);      $blockMinutes = $attempts === 1 ? 1 : ($attempts - 1) * $minutes;     $cache->add($key.':timer', $rateLimiter->availableAt($blockMinutes * 60), $blockMinutes);     $added = $cache->add($key, 0, $blockMinutes);     $hits = (int) $cache->increment($key, $attempts);     if (! $added && $hits === 1) {         $cache->put($key, 1, $blockMinutes);     }     $property->setAccessible(false);      $this->fireLockoutEvent($request);     return $this->sendLockoutResponse($request); } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: