From 11dfca59b1d816f7ea76348a812f80a700719025 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Wed, 28 Oct 2026 19:24:09 +0100 Subject: [PATCH] fix: remember a socket's user after token auth The package resolves the per-message authtoken for that message only. Recording socket_ and the authed-user map lets the server authenticate later messages itself and the game:events worker find sockets to push to. Co-Authored-By: Claude Opus 5.5 --- app/Websocket/GameController.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/app/Websocket/GameController.php b/app/Websocket/GameController.php index 59f19b7..58fd5f8 100644 --- a/app/Websocket/GameController.php +++ b/app/Websocket/GameController.php @@ -3,6 +3,7 @@ namespace App\Websocket; use App\Game\GameRuleViolation; +use BlaxSoftware\LaravelWebSockets\Services\WebsocketService; use BlaxSoftware\LaravelWebSockets\Websocket\Controller; use Closure; @@ -15,6 +16,25 @@ abstract class GameController extends Controller { public $need_auth = true; + /** + * The client sends its bearer token as `authtoken` with every message, which the + * package resolves through Sanctum for that message only. Remember the socket's + * user, so the server authenticates its next messages itself (`socket_`) and + * the game:events worker can find the owner's sockets for pushes. + */ + public function booted() + { + $user = auth()->user(); + $socketId = $this->connection->socketId ?? null; + if (! $user || ! $socketId) { + return; + } + cache()->put("socket_$socketId", ['id' => $user->id, 'type' => $user::class], now()->addDay()); + if ((WebsocketService::getAuthedUsers()[$socketId] ?? null) !== $user->id) { + WebsocketService::setUserAuthed($socketId, $user); + } + } + protected function play(Closure $intent): mixed { try {