Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Authentication Log Laravel Package

yadahan/laravel-authentication-log

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require yadahan/laravel-authentication-log
    
  2. Publish Assets:

    php artisan vendor:publish --provider="Yadahan\AuthenticationLog\AuthenticationLogServiceProvider"
    
    • This generates:
      • Database migration (authentication_logs table)
      • Config file (config/authentication-log.php)
      • Optional views (if using notifications)
  3. Run Migration:

    php artisan migrate
    
  4. First Use Case: Enable logging in config/authentication-log.php:

    'enabled' => env('AUTH_LOG_ENABLED', true),
    

    Verify logs appear in the authentication_logs table after user logins/logouts.


Implementation Patterns

Core Workflows

  1. Logging Authentication Events:

    • The package automatically logs:
      • login (success/failure)
      • logout
      • password-reset
      • failed-login (with IP/attempts)
    • Trigger Manually (if needed):
      use Yadahan\AuthenticationLog\Facades\AuthenticationLog;
      
      AuthenticationLog::login($user, $ip);
      AuthenticationLog::logout($user, $ip);
      
  2. Notifications:

    • Send alerts via authentication-log:send-notification event.
    • Extend Notifications:
      // config/authentication-log.php
      'notifications' => [
          'login' => \App\Notifications\LoginAlert::class,
          'logout' => \App\Notifications\LogoutAlert::class,
      ],
      
  3. IP Tracking:

    • Logs IP addresses by default. Disable in config:
      'log_ip' => false,
      
  4. User-Agent Logging:

    • Enable/disable via config:
      'log_user_agent' => true,
      

Integration Tips

  • Guard-Specific Logging: Use auth:guard middleware to log for specific guards:

    Route::middleware(['auth:admin'])->group(function () {
        // Admin-specific auth logs
    });
    
  • Custom Fields: Add extra metadata via event listeners:

    AuthenticationLog::extend(function ($log) {
        $log->setCustomData(['device' => request()->userAgent()]);
    });
    
  • API Logging: Combine with laravel-api-logging for unified audit trails.


Gotchas and Tips

Pitfalls

  1. Migration Conflicts:

    • If authentication_logs table exists, manually check for column mismatches (e.g., user_agent or custom_data fields).
  2. Performance:

    • Disable logging for high-traffic APIs:
      'enabled' => env('APP_ENV') !== 'production',
      
  3. IP Spoofing:

    • Trusted proxies (e.g., Cloudflare) may log incorrect IPs. Configure trusted_proxies in Laravel’s AppServiceProvider:
      $this->middleware(function ($request, $next) {
          if (request()->ip() !== request()->server('REMOTE_ADDR')) {
              request()->setUserResolver(function () {
                  return auth()->user();
              });
          }
          return $next($request);
      });
      
  4. Notification Delays:

    • Use queues for async notifications:
      'notifications' => [
          'login' => [
              'class' => \App\Notifications\LoginAlert::class,
              'queue' => 'high',
          ],
      ],
      

Debugging

  • Missing Logs: Verify auth.log middleware is registered in app/Http/Kernel.php:

    protected $middleware = [
        \Yadahan\AuthenticationLog\Middleware\AuthLogMiddleware::class,
    ];
    
  • Custom Data Not Saved: Ensure custom_data column exists in the migration or update it:

    $table->json('custom_data')->nullable();
    

Extension Points

  1. Custom Log Models: Extend the Yadahan\AuthenticationLog\Models\AuthenticationLog model:

    class CustomAuthLog extends AuthenticationLog {
        protected $table = 'custom_auth_logs';
    }
    

    Bind it in AuthServiceProvider:

    AuthenticationLog::setModel(CustomAuthLog::class);
    
  2. Event Overrides: Listen to authentication-log:before-log to modify logs:

    AuthenticationLog::before(function ($log) {
        $log->ip = request()->ip();
        $log->user_agent = request()->userAgent();
    });
    
  3. API Responses: Return logs in API responses (e.g., for admin dashboards):

    use Yadahan\AuthenticationLog\Facades\AuthenticationLog;
    
    return AuthenticationLog::getLogs()->paginate(10);
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope