Forked from tprinty/Adding Logging to Laravel Authentication
Created
September 11, 2022 13:58
-
-
Save gemarkalmacen/e11f60a98f01ccc075604d235b6e10e1 to your computer and use it in GitHub Desktop.
Revisions
-
tprinty renamed this gist
Aug 27, 2022 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
tprinty renamed this gist
Aug 27, 2022 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
tprinty renamed this gist
Aug 26, 2022 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
tprinty created this gist
Aug 26, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(); // } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); } }