From dcf8315290cc5fe0b286c9fe620bc7efe440b4d8 Mon Sep 17 00:00:00 2001 From: a6a2f5842 Date: Wed, 15 Oct 2025 09:27:37 +0200 Subject: [PATCH] I auth state --- src/Websocket/Handler.php | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Websocket/Handler.php b/src/Websocket/Handler.php index ac7797e..a6389aa 100644 --- a/src/Websocket/Handler.php +++ b/src/Websocket/Handler.php @@ -134,7 +134,7 @@ class Handler implements MessageComponentInterface ])); } - $this->authenticateConnection($connection, $channel, $message); + $this->authenticateConnection($connection, $channel); \Log::channel('websocket')->info('[' . $connection->socketId . ']@' . $channel->getName() . ' | ' . json_encode($message)); @@ -150,6 +150,7 @@ class Handler implements MessageComponentInterface $pid = pcntl_fork(); if ($pid == -1) { + Auth::logout(); Log::error('Fork error'); } elseif ($pid == 0) { try { @@ -181,6 +182,7 @@ class Handler implements MessageComponentInterface exit(0); } else { + Auth::logout(); $this->addDataCheckLoop($connection, $message, $pid); } } catch (\Throwable $e) { @@ -190,6 +192,7 @@ class Handler implements MessageComponentInterface 'trace' => $e->getTraceAsString(), ]); } + } /** @@ -197,6 +200,8 @@ class Handler implements MessageComponentInterface */ public function onClose(ConnectionInterface $connection): void { + $this->authenticateConnection($connection, null); + // remove connection from $channel_connections foreach ($this->channel_connections as $channel => $connections) { if (in_array($connection->socketId, $connections)) { @@ -228,6 +233,10 @@ class Handler implements MessageComponentInterface $authed_users = cache()->get('ws_socket_authed_users') ?? []; unset($authed_users[$connection->socketId]); cache()->forever('ws_socket_authed_users', $authed_users); + + \BlaxSoftware\LaravelWebSockets\Services\WebsocketService::clearUserAuthed( + $connection->socketId + ); } $this->channelManager @@ -241,6 +250,8 @@ class Handler implements MessageComponentInterface cache()->forget('ws_connection_' . $connection->socketId); } }); + + Auth::logout(); } /** @@ -468,7 +479,6 @@ class Handler implements MessageComponentInterface protected function authenticateConnection( ConnectionInterface $connection, PrivateChannel|Channel|PresenceChannel|null $channel, - $message ) { if (! optional($connection)->auth && $connection->socketId && cache()->get('socket_' . $connection->socketId)) { @@ -477,13 +487,17 @@ class Handler implements MessageComponentInterface $connection->user = @$cached_auth['type']::find($cached_auth['id']); - $channel->saveConnection($connection); + if($channel){ + $channel->saveConnection($connection); + } } // Update last online of user if user if (! optional($connection)->user) { $connection->user = false; - $channel->saveConnection($connection); + if($channel){ + $channel->saveConnection($connection); + } } // Set auth or logout @@ -506,6 +520,11 @@ class Handler implements MessageComponentInterface $authed_users[$connection->socketId] = $user->id; cache()->forever('ws_socket_authed_users', $authed_users); } + + // add next in loop logout + $this->channelManager->loop->futureTick(function () { + Auth::logout(); + }); } private function addDataCheckLoop(