diff options
| author | Sam Light <samlight1994@gmail.com> | 2025-04-10 00:40:03 +0100 | 
|---|---|---|
| committer | Sam Light <samlight1994@gmail.com> | 2025-04-10 00:40:03 +0100 | 
| commit | 35a5ec6c119c5acc8f25997187affd228d680ffb (patch) | |
| tree | 07dc7e0adbbbe609fc75f50eabdb2fe42b953b50 /src | |
| parent | 704e4c63dcf449641c5b98dc5433958554b96a91 (diff) | |
Created AuthSubscriber to listener to auth events and log them
Diffstat (limited to 'src')
| -rw-r--r-- | src/AuthSubscriber.php | 48 | 
1 files changed, 48 insertions, 0 deletions
| diff --git a/src/AuthSubscriber.php b/src/AuthSubscriber.php new file mode 100644 index 0000000..6d4cc63 --- /dev/null +++ b/src/AuthSubscriber.php @@ -0,0 +1,48 @@ +<?php + +namespace Lightscale\LaralightAuthLog; + +use Lightscale\LaralightAuthLog\Models\AuthLog; +use Lightscale\LaralightAuthLog\Enums\Status; + +use Illuminate\Auth\Events\Login; +use Illuminate\Auth\Events\Failed; +use Illuminate\Auth\Events\Logout; +use Illuminate\Events\Dispatcher; + +class AuthSubscriber +{ + +    private function handleEvent(Status $status, mixed $event): void +    { +        $log = new AuthLog([ +            'status' => $status, +        ]); +        $log->user()->associate($event->user); +        $log->save(); +    } + +    public function handleLogin(Login $event): void +    { +        $this->handleEvent(Status::LoginSuccess, $event); +    } + +    public function handleFailed(Failed $event): void +    { +        $this->handleEvent(Status::LoginFailure, $event); +    } + +    public function handleLogout(Logout $event): void +    { +        $this->handleEvent(Status::Logout, $event); +    } + +    public function subscribe(Dispatcher $events): array +    { +        return [ +            Login::class => 'handleLogin', +            Failed::class => 'handleFailed', +            Logout::class => 'handleLogout', +        ]; +    } +} | 
