I use the included authentication of laravel 5.1.6 and want to know how I can extend it, to work like this:
if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {
// The user is active, not suspended, and exists.
}
If the user is not "active", the login should not be possible. I have an 'active' column in the users table , with 0 or 1 as value. How can i do this while still using the built in authentication with login throtteling.
edit:
I don't have a postLogin function in the AuthController, only a use AuthenticatesAndRegistersUsers, ThrottlesLogins;
, a __construct()
, a validator()
and a create()
function. Do I have to change something in the trait in Illuminate\Foundation\Auth\..
or must I add the the postLogin()
function in the AuthController ?
You can just override the
getCredentials()
method in your AuthController:This will add the
active = 1
constraint when trying to authenticate a user.EDIT: If you want a separate error message like BrokenBinary says, then Laravel allows you to define a method called
authenticated
that is called after a user has been authenticated, but before the redirect, allowing you to do any post-login processing. So you could utilise this by checking if the authenticated user is active, and throw an exception or display an error message if not:Don’t forget to import the
Request
class andUser
model class.On Laravel
5.3.*
updateapp/Http/Controllers/Auth/LoginController
Solved: this link ( tutorial) will help you : https://medium.com/@mshanak/solved-tutorial-laravel-5-3-disable-enable-block-user-login-web-passport-oauth-4bfb74b0c810
step1:
step2:
Step3:
Done :)
I would add following first thing in
postLogin()
function.active
is a flag in user table. 0 = Inactive, 1 = active. so whole function would look like following..I have now changed the auth middleware
/app/Http/Middleware/Authenticate.php
(added the block below the comment):It seems, it also logs out inactive users if they were already logged in.