Luka Isailovic
Luka Isailovic

Reputation: 126

Laravel Authentication Custom Rule

I'm working with laravel Auth. Trying to add new rule with email and password, if status(field in user model) is = 1 then he cannot login. I cannot find where should i add this. I was looking at middleware, guard.php AuthenticateUsers.php but did not found it..


Edit:

I've solved this by creating new middleware that checks for this field. Also it can be done with Auth::attempt

Upvotes: 2

Views: 2531

Answers (2)

Prisacari Dmitrii
Prisacari Dmitrii

Reputation: 2106

Add this code to your LoginController:

/**
 * Attempt to log the user with custom credentials into the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return bool
 */
protected function attemptLogin(Request $request)
{
    $credentials = $this->credentials($request);

    $credentials['status'] = 1; // Additional field you want to check

    return $this->guard()->attempt(
        $credentials, $request->filled('remember')
    );
}

Here we add the status field as a part of checked credentials. If user has status not equal to 1, authentication will fail.

Works with Laravel5.6

Upvotes: 0

Amit Gupta
Amit Gupta

Reputation: 17658

You can try as:

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

From the Docs

If you wish, you also may add extra conditions to the authentication query in addition to the user's e-mail and password.

Upvotes: 2

Related Questions