I'm currently working on my Laravel app and to prevent spam I decided that only active users are able to login. I'm currently using Laravel's login system just like in Laravel's official website tutorial, here's my form action:
<form class="form-horizontal" role="form" method="POST" action="{{ url('/auth/login') }}">
This works completely fine, however I'd like to check the user's active, if not active it would be redirected to the activation page, otherwise it would login. Is there a simple way to do this or am I obligated to make a new controller, routes and more verifications? Thank you.
Edit: Forgot to mention that I have a 'active' column in my database.
This solution is based on Can Celik's idea and was tested with Laravel 5.3.
The last two comma-separated parameters (
active,1
) act as a WHERE clause (WHERE active = '1'
) and can be alternatively written this way:Normally, the validation method only checks if email and password fields are filled out. With the modification above we require that a given email address is found in a DB row with
active
value set to 1.UPDATE (tested with Laravel 5.5):
You can also customize the message:
Note that the above message will be shown both when a given email address doesn't exist or when the account is disabled.
If someone uses ajax request on login and wants to have custom message, here is how I achieved this in login controller:
login() function
And other functions
You don't have to override the whole function. You can just change the Validator in AuthController to achieve that adding "exists:table,column" validation.
Let's assume that you have a users table with email,password and active fields.
Here is the validotor function should look like in AuthController.php
or if you are using soft deletes this should work too.
You can also check out the validation rule at this link http://laravel.com/docs/5.1/validation#rule-exists
Thanks @Can_Celik
this was how I was able to solve my issue becos i was using
json response
with jquery.then in the
validation.php
file add this to your Custom Validation stringsthat's about all...works fine ...
I check user is actived by overwrite sendLoginResponse function in LoginController
In case anyone is came here looking for information on Laravel 5.4/5.5, and that allows for a custom message just for this scenario (not a combined message) here's the answer for that from https://laracasts.com/discuss/channels/laravel/user-account-status
Override the 'authenticated' method within your'app/Http/Controllers/Auth/LoginController.php` file: