edmond edmond - 4 months ago 81x
SQL Question

How to sign in an active user in Laravel 5.2?

I have a users table which holds every detail of the user including a field called 'isActive' which is a Boolean.

I want to log in the user if the 'isActive = 1' and also i want to redirect the user back to the login page if the 'isActive = 0'.

My code is below:

public function postSignIn(Request $request)

$this->validate($request, [
'username' => 'required',
'password' => 'required'

if(Auth::attempt(['username' => $request['username'], 'password' => $request['password']])) {
return redirect()->route('home');

The code above allows me to sign normally. With or without the isActive field being called.


As per Documentation,

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

In your case, you just need to add 'isActive' => 1 in array.

Your code should be:-

if (Auth::attempt(['username' => $request['username'], 'password' => $request['password'],'isActive' => 1])) {
    // The user is active, not suspended, and exists.
   // The user is Inactive, suspended, or not exists.
    return redirect()->route('home');