Skip to content

Commit

Permalink
Fix null reference error for 'username' property in WebhookListener
Browse files Browse the repository at this point in the history
- Added null check for auth()->user() in handleUserDeleted, handleEggCreated, and handleEggDeleted methods.
- Replaced 'username' with 'Unknown' if no user is authenticated during webhook events.
  • Loading branch information
Poseidon281 committed Aug 2, 2024
1 parent 7e9d0a9 commit dc68777
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions app/Listeners/WebhookListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected function handleUserDeleted($event)
$appUrl = env('APP_URL');
$currentTime = Carbon::now()->toDateTimeString();
$color = hexdec(env('DISCORD_EMBED_COLOR')) ?: 7423;
$admin = auth()->user()->username;
$admin = auth()->check() ? auth()->user()->username : 'Unknown';

if (env('WEBHOOK_TYPE') === 'json') {
$message = [
Expand Down Expand Up @@ -140,7 +140,7 @@ protected function handleEggCreated($event)
$author = $event->egg->author;
$name = $event->egg->name;
$description = $event->egg->description;
$admin = auth()->user()->username;
$admin = auth()->check() ? auth()->user()->username : 'Unknown';
$appName = env('APP_NAME');
$appUrl = env('APP_URL');
$currentTime = Carbon::now()->toDateTimeString();
Expand Down Expand Up @@ -192,7 +192,7 @@ protected function handleEggDeleted($event)
$author = $event->egg->author;
$name = $event->egg->name;
$description = $event->egg->description;
$admin = auth()->user()->username;
$admin = auth()->check() ? auth()->user()->username : 'Unknown';
$appName = env('APP_NAME');
$appUrl = env('APP_URL');
$currentTime = Carbon::now()->toDateTimeString();
Expand All @@ -210,7 +210,7 @@ protected function handleEggDeleted($event)
$this->send(
'user',
[
'event' => 'Egg Added',
'event' => 'Egg Deleted',
'triggered_at' => $currentTime,
'data' => $message,
]
Expand All @@ -223,8 +223,8 @@ protected function handleEggDeleted($event)
'icon_url' => 'https://pelican.dev/img/logo.png',
'url' => $appUrl,
],
'title' => 'Egg Added',
'description' => "ID: $ID\nName: $name\nEgg Author: $author\nDescription: $description\n\nAdded by: $admin",
'title' => 'Egg Deleted',
'description' => "ID: $ID\nName: $name\nEgg Author: $author\nDescription: $description\n\nDeleted by: $admin",
'color' => $color,
'footer' => [
'text' => "Current Time: $currentTime",
Expand Down

0 comments on commit dc68777

Please sign in to comment.