'Login if user is active using Laravel Breeze
I've changed it in the LoginRequest.php file at authenticate function, but it doesn't work.
if (! Auth::attempt($this->only('email', 'password', ['active' => 1]), $this->filled('remember'))) {
RateLimiter::hit($this->throttleKey());
throw ValidationException::withMessages([
'email' => __('auth.failed'),
]);
}
Solution 1:[1]
The active part needs to be added to the array passed to attempt, which needs to be merged with the array returned from only:
Auth::attempt($this->only('email', 'password') + ['active' => 1]) ...
Solution 2:[2]
if you want to check if user is active and then login the user you can check that just inside auth::attempt
like this:
if (Auth::attempt([...], $remember_me)) {
if (Auth::user()->is_active == 1) {
user-will-stay-login
}
else{
User::logout();
return \redirect()->back();
}
}
Solution 3:[3]
I tried this :
public function authenticate()
{
$this->ensureIsNotRateLimited();
if (Auth::attempt($this->only('email', 'password'), $this->boolean('remember'),)) {
if (Auth::user()->isActive != 1) {
Auth::logout();
throw ValidationException::withMessages([
'email' => __('auth.suspend'),
]);
}
} else {
RateLimiter::hit($this->throttleKey());
throw ValidationException::withMessages([
'email' => __('auth.failed'),
]);
}
RateLimiter::clear($this->throttleKey());
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | lagbox |
| Solution 2 | milad hedayatpoor |
| Solution 3 | alexistdev |
