Skip to content

Commit

Permalink
Add user id from request
Browse files Browse the repository at this point in the history
  • Loading branch information
Tjardo committed Jan 10, 2022
1 parent 66da0d0 commit da3e781
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Listeners/LogAuthAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function handle($event, array $context = null): void
DB::connection(config('authlog.database_connection'))->table(config('authlog.table_name'))->insert([
'event_name' => class_basename($event),
'email' => $this->getEmailParameter($event),
'user_id' => isset($event->user) ? $event->user->id : null,
'user_id' => $this->getUserIdParameter($event),
'ip_address' => Request::ip(),
'user_agent' => Request::userAgent(),
'context' => is_array($context) ? json_encode($context) : null,
Expand All @@ -41,4 +41,18 @@ private function getEmailParameter($event): ?string

return null;
}

/** @param mixed $event */
private function getUserIdParameter($event): ?string
{
if (isset($event->user)) {
return $event->user->id;
}

if (Request::user()) {
return Request::user()->id;
}

return null;
}
}

0 comments on commit da3e781

Please sign in to comment.