I have been trying to add a requirement to my LoginController that a user must be 'active' to log in. I know there are plenty of projects and scenarios where this is needed, but I can't find some definite answers on how Laravel wants this implemented.
In the documentation it says this:
-
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.
}
This is great, and certainly helpful. But the message that comes back is:
'failed' => 'These credentials do not match our records.'
This isn't accurate, because the credentials are correct. I could change it to say "These credentials do not match our records, or your account is disabled" but then the user wouldn't know which is which.
What I really want is the user to know that the authentication failed because they are disabled.
I have tried overriding sendFailedLoginResponse() to include the active field in the ValidationException like this:
protected function sendFailedLoginResponse(Request $request)
{
throw ValidationException::withMessages([
$this->username() => [trans('auth.failed')],
'active' => [trans('auth.inactive_account')]
]);
}
lang/en/auth.php:
'failed' => 'These credentials do not match our records.',
'throttle' => 'Too many login attempts. Please try again in :seconds seconds.',
'inactive_account' => 'This account is no longer active.'
attemptLogin():
protected function attemptLogin(Request $request)
{
return $this->guard()->attempt(
array_merge(
$this->credentials($request),
['active' => 1]
),
$request->filled('remember')
);
}
But now what happens is, even when the credentials are wrong, it always says the 'inactive_account' message.
Hoping to get some help here. I have seen solutions where the account is queried first, and then redirected back if the 'active' column is false. I feel this breaks the proper workflow of the AuthenticatesUsers trait.