Did you know that Laravel Auth system allows you to block the user after X bad attempts to log in? Even more, you can change that limit! This trick works with out of the box Laravel Auth system and all you have to do is modify one file.
Open App\Http\Controllers\Auth\AuthController.php and add these lines:
protected $maxLoginAttempts = 10; // Amount of bad attempts user can make protected $lockoutTime = 300; // Time for which user is going to be blocked in seconds
Now if we enter our bad login information 10 times in a row, we should see:
By default, if you don’t change anything and just use Laravel out-of-the-box, those parameters are equal to 5 times and 60 seconds respectively.
To change the error message we see, go to resources/lang/en/auth.php.
'failed' => 'These credentials do not match our records.', 'throttle' => 'Too many login attempts. Please try again in :seconds seconds.',
If you’re curious how it works under the hood, it’s simple: information about blocked users and remaining time is stored in session data. Not in database or cookies, in session.
Doesn’t the storage in session data also mean an attacker only needs to not send the session id on each subsequent request, in order to bypass this mechanism? Or, alternatively, s/he just needs to use botnet to bruteforce login without ever being locked out? Storing the attempts in the database would make these attacks useless.
Yes and no at the same time. In many cases, even the database case, you would have to attach a user id to specific user via cookies. This means that if I clear the cookies – I bypass any solution. Unless your app depends on IP address (which is really bad and should not be done), then yea – they would have a bit more problems. So in short you can bypass this system or any other just by clearing cookies. On the other hand if the auth mechanism would work with one exact user – yea, database is a must there to block the single user but then complexity would come and for example if I try to log in into my account and blocked message would pop – I would not understand it.
Modestas Vaikevicius, can you explain why using an IP address to do rate limiting is “really bad and should not be done”?
I have the same question as @Hyperflux :
Why is limiting access by IP a bad idea? Actually, I am not sure that is what you meant by, “Unless your app depends on IP address (which is really bad and should not be done)”
Can you explain further?
Thanks for your work here.
It is a bad idea because many users could be behind the same IP address. A library for example. Or you at home with a mate surfing the web.. same IP to the outside world. So it is a bad idea to identify someone by it’s IP only…
Thanks for the post. What version of Laravel is this example for? I tried in 5.0* and it doesn’t seem to work.
Okay, I see this was added in 5.1, thanks anyway.
Its not working sir.
Hey fri.
Have you solved this problem.
please explain me please.
this code is not work for me
Can i Set $lockoutTime in Minutes like
$lockoutTime = 5; //// Time in Minutes
It’s not working in laravel 5.2 I have added the traits. and that’s it.
Do I need to do anything or add any middleware on the auth/login (post) route?
Please reply thanks
Thanks a lot Povilas.
Your tutorial saved me lot of hours.
Is there a way to show a message like “Please change your password” to user after few months of his or her first login? Password never expire. But just show a message.
Thanks again.
Thanks!!
Its Working for me. It saved lot my time and efforts.
it does not work for me
what laravel version did you use?
Povilas, your lesson here is relevant to up to Laravel 5.2.
It does not work in 5.3 and 5.4 where it is hardcoded.
I do not want to steal your thunder, but it is too much to past it here, so just a link: http://s4.jeffsbio.co.uk/laravel-5-security-authentication-lets-bring-back-maxloginattempts-and-lockouttime
I am not sure about your “link in post policy”.
If this violates it, just remove my post.
I have implemented User Activation via email with Laravel 5.2
il work fine, but I have probmeme when user not clicked on the link of validation inside email verification after some time for exemple 1 or 2 hours.