Skip to content

Instantly share code, notes, and snippets.

@valorin
Last active July 27, 2024 11:26
Show Gist options
  • Save valorin/b90dc36197f47323a9207093f6a6dfc5 to your computer and use it in GitHub Desktop.
Save valorin/b90dc36197f47323a9207093f6a6dfc5 to your computer and use it in GitHub Desktop.
Login Logging for LSID
<?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()
{
}
}
<?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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment