I auth state

This commit is contained in:
a6a2f5842 2025-10-15 09:27:37 +02:00
parent 76d6b30fdf
commit dcf8315290
1 changed files with 23 additions and 4 deletions

View File

@ -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)); \Log::channel('websocket')->info('[' . $connection->socketId . ']@' . $channel->getName() . ' | ' . json_encode($message));
@ -150,6 +150,7 @@ class Handler implements MessageComponentInterface
$pid = pcntl_fork(); $pid = pcntl_fork();
if ($pid == -1) { if ($pid == -1) {
Auth::logout();
Log::error('Fork error'); Log::error('Fork error');
} elseif ($pid == 0) { } elseif ($pid == 0) {
try { try {
@ -181,6 +182,7 @@ class Handler implements MessageComponentInterface
exit(0); exit(0);
} else { } else {
Auth::logout();
$this->addDataCheckLoop($connection, $message, $pid); $this->addDataCheckLoop($connection, $message, $pid);
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
@ -190,6 +192,7 @@ class Handler implements MessageComponentInterface
'trace' => $e->getTraceAsString(), 'trace' => $e->getTraceAsString(),
]); ]);
} }
} }
/** /**
@ -197,6 +200,8 @@ class Handler implements MessageComponentInterface
*/ */
public function onClose(ConnectionInterface $connection): void public function onClose(ConnectionInterface $connection): void
{ {
$this->authenticateConnection($connection, null);
// remove connection from $channel_connections // remove connection from $channel_connections
foreach ($this->channel_connections as $channel => $connections) { foreach ($this->channel_connections as $channel => $connections) {
if (in_array($connection->socketId, $connections)) { if (in_array($connection->socketId, $connections)) {
@ -228,6 +233,10 @@ class Handler implements MessageComponentInterface
$authed_users = cache()->get('ws_socket_authed_users') ?? []; $authed_users = cache()->get('ws_socket_authed_users') ?? [];
unset($authed_users[$connection->socketId]); unset($authed_users[$connection->socketId]);
cache()->forever('ws_socket_authed_users', $authed_users); cache()->forever('ws_socket_authed_users', $authed_users);
\BlaxSoftware\LaravelWebSockets\Services\WebsocketService::clearUserAuthed(
$connection->socketId
);
} }
$this->channelManager $this->channelManager
@ -241,6 +250,8 @@ class Handler implements MessageComponentInterface
cache()->forget('ws_connection_' . $connection->socketId); cache()->forget('ws_connection_' . $connection->socketId);
} }
}); });
Auth::logout();
} }
/** /**
@ -468,7 +479,6 @@ class Handler implements MessageComponentInterface
protected function authenticateConnection( protected function authenticateConnection(
ConnectionInterface $connection, ConnectionInterface $connection,
PrivateChannel|Channel|PresenceChannel|null $channel, PrivateChannel|Channel|PresenceChannel|null $channel,
$message
) { ) {
if (! optional($connection)->auth && $connection->socketId && cache()->get('socket_' . $connection->socketId)) { if (! optional($connection)->auth && $connection->socketId && cache()->get('socket_' . $connection->socketId)) {
@ -477,14 +487,18 @@ class Handler implements MessageComponentInterface
$connection->user = @$cached_auth['type']::find($cached_auth['id']); $connection->user = @$cached_auth['type']::find($cached_auth['id']);
if($channel){
$channel->saveConnection($connection); $channel->saveConnection($connection);
} }
}
// Update last online of user if user // Update last online of user if user
if (! optional($connection)->user) { if (! optional($connection)->user) {
$connection->user = false; $connection->user = false;
if($channel){
$channel->saveConnection($connection); $channel->saveConnection($connection);
} }
}
// Set auth or logout // Set auth or logout
($connection->user) ($connection->user)
@ -506,6 +520,11 @@ class Handler implements MessageComponentInterface
$authed_users[$connection->socketId] = $user->id; $authed_users[$connection->socketId] = $user->id;
cache()->forever('ws_socket_authed_users', $authed_users); cache()->forever('ws_socket_authed_users', $authed_users);
} }
// add next in loop logout
$this->channelManager->loop->futureTick(function () {
Auth::logout();
});
} }
private function addDataCheckLoop( private function addDataCheckLoop(