Login a user only if his status is active in Laravel 5.7

Debajeet Choudhury picture Debajeet Choudhury · Dec 14, 2018 · Viewed 7.4k times · Source

I am new to Laravel and have been fairly successful in implementing user authentication. Now to move on to the next step I must allow only users whose status in active to login. For that I have added a

status TINYINT

column in my mysql users table.

I found this in the Laravel Documentation:

Specifying Additional Conditions

If you wish, you may also add extra conditions to the authentication query in addition to the user's e-mail and password. For example, we may verify that user is marked as "active":

if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {
// The user is active, not suspended, and exists.
}

Can someone please point out where I need to put this chunk. Am thoroughly confused and need some pointers.

Thanks

Answer

kapitan picture kapitan · Dec 14, 2018

Have this on your LoginController:

protected function credentials(Request $request)
{        
   return ['username' => $request->{$this->username()}, 'password' => $request->password, 'status' => 1];
}