summaryrefslogtreecommitdiff
path: root/src/AuthSubscriber.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/AuthSubscriber.php')
-rw-r--r--src/AuthSubscriber.php48
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',
+ ];
+ }
+}