Installation
composer require lab404/laravel-auth-checker
php artisan vendor:publish --provider="Lab404\AuthChecker\AuthCheckerServiceProvider" --tag="migrations"
php artisan migrate
auth_checker_logs and auth_checker_devices tables.Configuration
php artisan vendor:publish --provider="Lab404\AuthChecker\AuthCheckerServiceProvider" --tag="config"
config/auth-checker.php:
lockout_threshold: Number of failed attempts before lockout (default: 5).lockout_duration: Duration of lockout in minutes (default: 15).log_successful_attempts: Track successful logins (default: true).log_ip_address: Track IP addresses (default: true).First Use Case: Logging Logins
Auth events.attempting, authenticated, and failed events.
// Example: Manually trigger logging (if needed)
use Lab404\AuthChecker\Facades\AuthChecker;
AuthChecker::logAttempt($user, $credentials, $isSuccessful);
auth.checker.logged or auth.checker.failed.AuthChecker::logDevice($user, [
'ip' => $request->ip(),
'user_agent' => $request->userAgent(),
'location' => 'Custom Location',
]);
lockout_threshold failed attempts.AuthChecker::lockUser($user, $durationMinutes = null); // Lock for X minutes
AuthChecker::unlockUser($user); // Force-unlock
if (AuthChecker::isUserLocked($user)) {
abort(403, 'Account locked due to too many failed attempts.');
}
$logs = AuthChecker::getUserLogs($user);
// Returns collection of attempts with timestamps, IPs, and success status.
$logs = AuthChecker::getLogs()
->where('created_at', '>', now()->subDays(7))
->where('ip_address', $request->ip())
->get();
use Lab404\AuthChecker\Middleware\CheckLockout;
Route::middleware([CheckLockout::class])->group(function () {
// Routes requiring lockout check
});
public function handle($request, Closure $next) {
if (AuthChecker::isUserLocked($request->user())) {
return redirect()->route('account.locked');
}
return $next($request);
}
AuthChecker::failed($request, $user, $credentials)
->then(function ($log) {
// Send email/notification on failed attempt
Notification::send($user, new FailedLoginNotification($log));
});
Migration Conflicts
users table, ensure the auth_checker_logs table’s user_id foreign key matches your users.id column.php artisan vendor:publish --tag="migrations" again and resolve conflicts manually.Lockout Bypassing
is_admin = true (or similar) might bypass lockouts if not explicitly checked.if ($request->user()->is_admin) {
return $next($request);
}
IP Spoofing
trusted_proxies in AuthCheckerConfig:
'trusted_proxies' => [
'192.168.1.1',
'10.0.0.1',
],
Performance on Large Logs
$logs = AuthChecker::getUserLogs($user)->paginate(10);
Log Levels
config/auth-checker.php to log only critical events:
'log_level' => 'critical', // Options: debug, info, warning, error, critical
Event Debugging
EventServiceProvider:
protected $listen = [
'auth.attempting' => [
'Lab404\AuthChecker\Listeners\LogAttemptListener',
],
];
Clear Old Logs
// app/Console/Commands/ClearAuthLogs.php
AuthChecker::getLogs()->where('created_at', '<', now()->subMonths(6))->delete();
Custom Fields
auth_checker_logs table via migrations:
Schema::table('auth_checker_logs', function (Blueprint $table) {
$table->string('custom_field')->nullable();
});
AuthCheckerLog model to cast the new field.Custom Lockout Logic
AuthChecker::extend(function ($app) {
$app->bind('auth.checker.lockout', function () {
return new CustomLockoutHandler();
});
});
Webhook Notifications
AuthChecker::failed($request, $user)
->then(function () use ($user) {
Http::post('https://your-webhook-url', [
'user_id' => $user->id,
'event' => 'lockout',
]);
});
Multi-Factor Auth (MFA) Integration
if ($request->user()->hasMfaEnabled()) {
AuthChecker::skipLockout($request);
}
auth.checker.logged to sync logins with OAuth providers.spatie/laravel-geolocation.laravel-rate-limiting to add CAPTCHA after X failed attempts.How can I help you explore Laravel packages today?