fix: remember a socket's user after token auth
The package resolves the per-message authtoken for that message only. Recording socket_<id> 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 <noreply@anthropic.com>
This commit is contained in:
parent
1cf6beab3d
commit
11dfca59b1
|
|
@ -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_<id>`) 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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue