rappasoft/laravel-authentication-log
Installation:
composer require rappasoft/laravel-authentication-log
php artisan vendor:publish --provider="Rappasoft\LaravelAuthenticationLog\LaravelAuthenticationLogServiceProvider" --tag="authentication-log-migrations"
php artisan migrate
Add Trait to User Model:
use Rappasoft\LaravelAuthenticationLog\Traits\AuthenticationLoggable;
class User extends Authenticatable
{
use AuthenticationLoggable;
}
First Use Case: Immediately start tracking logins/logouts by attempting to authenticate a user. The package will automatically log:
config/authentication-log.php (publish with --tag="authentication-log-config")database/migrations/[timestamp]_create_authentication_logs_table.phpapp/Models/AuthenticationLog.php (if extending)php artisan list (look for authentication-log:* commands)// Automatic - No manual intervention needed
// Logs are created on successful/failed login attempts
// Access logs via:
$user->authenticationLogs()->latest()->first();
// Trust a device (e.g., after user verification)
$user->trustDevice($deviceId);
// Get all devices
$devices = $user->getDevices();
// Check if current device is trusted
if ($user->isDeviceTrusted()) {
// Proceed with sensitive actions
}
// In a controller after login
$user->revokeAllOtherSessions(request()->ip()); // Keep current session
// In admin panel
$activeSessions = $user->getActiveSessions();
foreach ($activeSessions as $session) {
if ($session->is_suspicious) {
$user->revokeSession($session->id);
}
}
// Check for suspicious activity during login
$suspicious = $user->detectSuspiciousActivity();
if ($suspicious) {
notify($user, new SuspiciousActivityDetected($suspicious));
}
// routes/web.php
Route::middleware(['auth', 'trusted.device'])->group(function () {
// Routes requiring trusted devices
});
// config/notifications.php
'channels' => [
'slack' => [
'driver' => 'slack',
// Configure Slack webhook
],
],
// In a controller or service
$recentFailedAttempts = AuthenticationLog::failed()
->recent(24)
->forUser($user)
->get();
// config/authentication-log.php
'webhooks' => [
[
'url' => env('AUTH_LOG_WEBHOOK_URL'),
'events' => ['login', 'failed', 'new_device'],
'secret' => env('AUTH_LOG_WEBHOOK_SECRET'),
],
],
// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('authentication-log:purge')->daily();
$schedule->command('authentication-log:export:failed-attempts')->weekly();
}
Duplicate Log Entries:
prevent_session_restoration_logging in config to true (default in v6+).is_session_restoration flag in logs.GeoIP Configuration:
torann/geoip and configure in config/authentication-log.php:
'geoip' => [
'enabled' => true,
'database' => database_path('GeoLite2-City.mmdb'),
],
Device Fingerprinting:
'device_fingerprint' => [
'normalize_browser_versions' => true,
],
Notification Spam:
'notifications' => [
'rate_limit' => 3, // per hour
],
Performance with Large Logs:
php artisan authentication-log:purgeSchema::table('authentication_logs', function (Blueprint $table) {
$table->index('user_id');
$table->index('ip_address');
$table->index(['user_id', 'created_at']);
});
Log Inspection:
// Check raw fingerprint data
$fingerprintData = $user->getLastLogin()->fingerprint_data;
// Debug GeoIP
\Torann\GeoIP\Facades\GeoIP::getLocation(request()->ip());
Suspicious Activity:
// Dump suspicious reasons
dd($user->detectSuspiciousActivity());
Webhook Testing:
telescope or laravel-debugbar to inspect outgoing requests.curl:
curl -X POST -H "Content-Type: application/json" \
-d '{"event":"login","data":{...}}' \
http://your-webhook-url
Custom Log Fields:
// Extend the log model
class AuthenticationLog extends \Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog
{
protected $casts = [
'custom_field' => 'boolean',
];
}
Custom Suspicious Activity Rules:
// Extend the suspicious activity detector
class CustomSuspiciousActivityDetector extends \Rappasoft\LaravelAuthenticationLog\Services\SuspiciousActivityDetector
{
public function detectCustomRule(User $user)
{
// Implement custom logic
return $this->createSuspiciousActivity('custom_rule', 'Custom suspicious activity detected');
}
}
Custom Notifications:
// Create a custom notification
class NewDeviceLoginNotification extends Notification
{
public function via($notifiable)
{
return ['mail', 'slack'];
}
}
// Override in config
'notifications' => [
'new_device' => \App\Notifications\NewDeviceLoginNotification::class,
],
Custom Query Scopes:
// Add to AuthenticationLog model
public function scopeFromCountry($query, $countryCode)
{
return $query->where('country_code', $countryCode);
}
Session Restoration Window:
5 minutes (configurable via session_restoration_window_minutes).0 to disable entirely.Device Trust Defaults:
Failed Login Notifications:
'notifications' => [
'failed_login' => true,
],
Webhook Events:
login, logout, failed, new_device, suspicious.Export Formatting:
hidden in model).How can I help you explore Laravel packages today?