如何在登录时配置用户禁止更多 maxAttempts?

How to configure users ban for more maxAttempts in login?

我在Laravel 7

工作

我根据需要调整loginController; maxAttempts 和 decayMinutes

public function maxAttempts()
{
        return General::first()->max_attempts;
}

public function decayMinutes()
{
        return General::first()->decay_minutes;
}

如何禁止用户超过 maxAttempts

示例 => maxAttempts = 4

我想禁止用户 5 次失败尝试

$user->is_block = true

您需要为用户创建一个列,用于统计他们的不成功登录尝试。在每次不成功的尝试中,您将增加此值并在达到特定限制时阻止用户。

如果登录成功,则将计数器设置为0。

我测试了一下,没错。

  • 首先在您的 EventServiceProvider 中创建两个事件侦听器。 登录成功登录失败
protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        'Illuminate\Auth\Events\Login' => [
            'App\Listeners\SuccessfulLogin',
        ],

        'Illuminate\Auth\Events\Failed' => [
            'App\Listeners\FailedLogin',
        ],
    ];
  • 在成功登录中:
public function handle(Login $event)
    {
        
        $event->user->user_last_login_date = Carbon::now();
        $event->user->unsuccessful_login_count = 0;
        $event->user->save();

    }
  • 登录失败:
$event->user->unsuccessful_login_count += 1 ;
        $unsuccessful_count =   General::first()->max_attempts;

        if ($event->user->unsuccessful_login_count == $unsuccessful_count ) {
            $event->user->three_attempt_timestamp = Carbon::now()->toDateString();
        }
        if ($event->user->unsuccessful_login_count > $unsuccessful_count ) {
            $event->user->is_block = 1;
        }

        $event->user->save();