gestazion/laravel-auth-checker
Installation
composer require gestazion/laravel-auth-checker
php artisan vendor:publish --provider="Gestazion\AuthChecker\AuthCheckerServiceProvider" --tag="migrations"
php artisan migrate
php artisan auth-checker:install (if provided) to set up default configurations.Configuration
php artisan vendor:publish --provider="Gestazion\AuthChecker\AuthCheckerServiceProvider" --tag="config"
config/auth-checker.php for:
log_auth_attempts: Enable/disable logging failed/successful attempts.lock_after_attempts: Set max failed attempts before locking an account.lock_duration: Duration (in minutes) for account lockout.allowed_ips: Whitelist IPs for bypassing checks (e.g., admin panels).First Use Case: Logging Auth Attempts
config/auth-checker.php:
'log_auth_attempts' => true,
POST /login. Check the auth_attempts table for records.Logging Authentication Events
auth_attempts table.use Gestazion\AuthChecker\Facades\AuthChecker;
AuthChecker::logAttempt($user, $isSuccessful, $ipAddress, $userAgent);
Locking Accounts After Failed Attempts
lock_after_attempts and lock_duration in the config.'lock_after_attempts' => 5,
'lock_duration' => 30,
if (AuthChecker::isLocked($user)) {
abort(429, 'Account locked. Try again later.');
}
IP-Based Restrictions
'allowed_ips' => ['192.168.1.0/24', '10.0.0.5'],
if (auth()->user()->isAdmin()) {
AuthChecker::allowIp($ipAddress);
}
Customizing Lockout Responses
LoginController:
public function login(Request $request)
{
if (AuthChecker::isLocked($request->user())) {
return back()->withError('Account locked. Contact support.');
}
// ... rest of login logic
}
Device Tracking
AuthChecker::logDevice($user, $ip, $userAgent);
$devices = AuthChecker::getUserDevices($user);
Integration with Laravel Events
auth.attempting, auth.failed, and auth.successful events:
Auth::attempting(function ($request) {
AuthChecker::logAttempt($request->user(), false, $request->ip());
});
Customizing the Auth Attempts Table
AuthAttempt model (e.g., add location or device_type):
php artisan make:model AuthAttemptExtension --extend=Gestazion\AuthChecker\Models\AuthAttempt
php artisan vendor:publish --tag="auth-checker-migrations"
Rate Limiting with Throttle
throttle middleware for additional protection:
Route::post('/login', [LoginController::class, 'login'])
->middleware(['throttle:5,1']);
Two-Factor Authentication (2FA) Integration
AuthChecker::logTwoFactorAttempt($user, $isSuccessful, $ipAddress);
Exporting Logs
use Gestazion\AuthChecker\Facades\AuthChecker;
AuthChecker::exportLogsToStorage($user, 'csv');
Database Overhead
queue to defer logging:
AuthChecker::queueLogAttempt($user, $isSuccessful, $ipAddress);
IP Spoofing
user_agent and geolocation (e.g., using geoip-database):
AuthChecker::logAttempt($user, false, $ip, $userAgent, $country);
Lockout Loops
lock_duration is too long. Tip:
public function unlock(Request $request) {
AuthChecker::unlock($request->user());
return back()->with('status', 'Account unlocked!');
}
temporary lock flag.Config Caching
config/auth-checker.php require cache clearing:
php artisan config:clear
Middleware Conflicts
auth middleware, ensure AuthChecker runs before session validation:
// app/Http/Middleware/Authenticate.php
public function handle($request, Closure $next, ...$guards) {
if (AuthChecker::isLocked($request->user())) {
abort(429);
}
return $next($request);
}
Check Logs
storage/logs/laravel.log for AuthChecker events.Test Lockout Manually
php artisan tinker
>>> $user = User::first();
>>> AuthChecker::lock($user, 5, 30); // Lock for 5 attempts, 30 mins
Verify Database Records
auth_attempts:
SELECT * FROM auth_attempts WHERE user_id = 1 ORDER BY created_at DESC;
Disable Logging Temporarily
'log_auth_attempts' => false in config to debug without cluttering logs.Custom Lockout Notifications
AuthChecker facade to send emails/SMS on lockout:
AuthChecker::extend(function ($checker) {
$checker->onLock(function ($user) {
Mail::to($user)->send(new AccountLocked($user));
});
});
Custom Validation Rules
AuthChecker for device fingerprinting (e.g., using laravel-fingerprint):
AuthChecker::validateDevice($user, $fingerprint);
API-Specific Logic
if ($request->is('api/*')) {
AuthChecker::setApiMode(true); // Stricter checks
}
Third-Party Integrations
AuthChecker::onFailedAttempt(function ($attempt) {
// Send to SIEM
$siem->log($attempt);
});
laravel-activitylog: Correlate auth events with other user actions.lock_after_attempts for user segments (e.g., admins vs. regular users).How can I help you explore Laravel packages today?