Created
February 21, 2024 13:18
-
-
Save AnandPilania/5c013739843d3a37224086cd4f087242 to your computer and use it in GitHub Desktop.
Laravel Auth logging
This file contains 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 characters
<?php | |
namespace App\Listeners; | |
use App\Events; | |
use Illuminate\Auth\Events as LaravelEvents; | |
use Illuminate\Support\Facades\Log; | |
class LogActivity | |
{ | |
public function login(LaravelEvents\Login $event) | |
{ | |
$this->info($event, "User {$event->user->email} logged in", $event->user->only('id', 'email')); | |
} | |
public function logout(LaravelEvents\Logout $event) | |
{ | |
$this->info($event, "User {$event->user->email} logged out", $event->user->only('id', 'email')); | |
} | |
public function registered(LaravelEvents\Registered $event) | |
{ | |
$this->info($event, "User registered: {$event->user->email}"); | |
} | |
public function failed(LaravelEvents\Failed $event) | |
{ | |
$this->info($event, "User {$event->credentials['email']} login failed", ['email' => $event->credentials['email']]); | |
} | |
public function passwordReset(LaravelEvents\PasswordReset $event) | |
{ | |
$this->info($event, "User {$event->user->email} password reset", $event->user->only('id', 'email')); | |
} | |
protected function info(object $event, string $message, array $context = []) | |
{ | |
$class = class_basename($event::class); | |
Log::info("[{$class}] {$message}", $context); | |
} | |
} |
This file contains 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 characters
<?php | |
namespace App\Providers; | |
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; | |
use Illuminate\Support\Facades\Event; | |
class EventServiceProvider extends ServiceProvider | |
{ | |
protected $listen = [ | |
\Illuminate\Auth\Events\Login::class => [ | |
\App\Listeners\AdoptPurchase::class, | |
\App\Listeners\RegisterForProduct::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', | |
] | |
]; | |
public function boot() | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment