PHP code example of gopal-gautam / failed-login-logger

1. Go to this page and download the library: Download gopal-gautam/failed-login-logger library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gopal-gautam / failed-login-logger example snippets


use GG\FailedLoginLogger\Models\FailedLoginAttempt;

// Recent attempts (last 60 minutes)
FailedLoginAttempt::recent()->latest()->get();

// Attempts for a specific email or IP
FailedLoginAttempt::forEmail('[email protected]')->count();
FailedLoginAttempt::fromIp('203.0.113.4')->recent(15)->count();

// Resolve the related user, when the identifier matched a known account
FailedLoginAttempt::with('user')->latest()->limit(20)->get();

$count = FailedLoginAttempt::fromIp(request()->ip())->recent(15)->count();

if ($count >= 10) {
    abort(429, 'Too many failed login attempts. Please try again later.');
}

use Illuminate\Support\Facades\Schedule;

Schedule::command('model:prune', [
    '--model' => [\GG\FailedLoginLogger\Models\FailedLoginAttempt::class],
])->daily();

// app/Models/FailedLoginAttempt.php
namespace App\Models;

use GG\FailedLoginLogger\Models\FailedLoginAttempt as BaseAttempt;

class FailedLoginAttempt extends BaseAttempt
{
    protected $appends = ['country'];

    public function getCountryAttribute(): ?string
    {
        return geoip()->getLocation($this->ip_address)->iso_code ?? null;
    }
}

// config/failed-login-logger.php
'model' => App\Models\FailedLoginAttempt::class,
bash
php artisan vendor:publish --tag=failed-login-logger-migrations
php artisan migrate
bash
php artisan vendor:publish --tag=failed-login-logger-config