Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gemarkalmacen/e11f60a98f01ccc075604d235b6e10e1 to your computer and use it in GitHub Desktop.
Save gemarkalmacen/e11f60a98f01ccc075604d235b6e10e1 to your computer and use it in GitHub Desktop.

Revisions

  1. @tprinty tprinty renamed this gist Aug 27, 2022. 1 changed file with 0 additions and 0 deletions.
  2. @tprinty tprinty renamed this gist Aug 27, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @tprinty tprinty renamed this gist Aug 26, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @tprinty tprinty created this gist Aug 26, 2022.
    46 changes: 46 additions & 0 deletions EventsServiceProvider.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    <?php

    namespace App\Providers;

    use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
    use Illuminate\Support\Facades\Event;

    class EventServiceProvider extends ServiceProvider
    {
    /**
    * The event listener mappings for the application.
    *
    * @var array
    */
    protected $listen = [
    \Illuminate\Auth\Events\Login::class => [

    \App\Listeners\LogActivity::class.'@login',
    ],
    \Illuminate\Auth\Events\Logout::class => [
    \App\Listeners\LogActivity::class.'@logout',
    ],
    \Illuminate\Auth\Events\Registered::class => [
    \Illuminate\Auth\Listeners\SendEmailVerificationNotification::class,
    \App\Listeners\LogActivity::class.'@registered',
    ],
    \Illuminate\Auth\Events\Failed::class => [
    \App\Listeners\LogActivity::class.'@failed',
    ],
    \Illuminate\Auth\Events\PasswordReset::class => [
    \App\Listeners\LogActivity::class.'@passwordReset',
    ]
    ];

    /**
    * Register any events for your application.
    *
    * @return void
    */
    public function boot()
    {
    parent::boot();

    //
    }
    }
    5 changes: 5 additions & 0 deletions Instructions
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    Here is a quick way to add authentication logging to Laravel.

    1. Modify app/Providers/EventServiceProvider.php and add lines 16 through 32 of the example file in this GIST.
    2. Create a new file app/Listeners/LogActivity.php and copy the contents of the file below into that file.
    3. Enjoy logging.
    48 changes: 48 additions & 0 deletions LogActivity.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    <?php
    namespace App\Listeners;

    use App\Events;
    use Request;
    use Illuminate\Auth\Events as LaravelEvents;
    use Illuminate\Support\Facades\Log;

    class LogActivity
    {
    public function login(LaravelEvents\Login $event)
    {
    $ip = \Request::getClientIp(true);
    $this->info($event, "User {$event->user->email} logged in from {$ip}", $event->user->only('id', 'email'));
    }

    public function logout(LaravelEvents\Logout $event)
    {
    $ip = \Request::getClientIp(true);
    $this->info($event, "User {$event->user->email} logged out from {$ip}", $event->user->only('id', 'email'));
    }

    public function registered(LaravelEvents\Registered $event)
    {
    $ip = \Request::getClientIp(true);
    $this->info($event, "User registered: {$event->user->email} from {$ip}");
    }

    public function failed(LaravelEvents\Failed $event)
    {
    $ip = \Request::getClientIp(true);
    $this->info($event, "User {$event->credentials['email']} login failed from {$ip}", ['email' => $event->credentials['email']]);
    }

    public function passwordReset(LaravelEvents\PasswordReset $event)
    {
    $ip = \Request::getClientIp(true);
    $this->info($event, "User {$event->user->email} password reset from {$ip}", $event->user->only('id', 'email'));
    }

    protected function info(object $event, string $message, array $context = [])
    {
    //$class = class_basename($event::class);
    $class = get_class($event);

    Log::info("[{$class}] {$message}", $context);
    }
    }