swooletw / laravel-swoole

High performance HTTP server based on Swoole. Speed up your Laravel or Lumen applications.
MIT License
4.04k stars 390 forks source link

laravel swoole websocket auth problem with socket.io-client v3 #498

Open SchahinRohani opened 3 years ago

SchahinRohani commented 3 years ago

Make sure you read Issues Guideline and answer these questions before submitting your issue. Thanks! (Any non-English issues will be closed immediately.)

  1. Please provide your PHP and Swoole version. (php -v and php --ri swoole)
  2. Please provide your Laravel/Lumen version.
  3. Which release version of this package are you using?
Software Version
PHP 8.0.8
Swoole 4.7.0
Laravel 8.51.0
laravel-swoole 2.8
  1. What did you do? If possible, provide a recipe for reproducing the error.

config/swoole_http.php

'providers' => [
    Illuminate\Pagination\PaginationServiceProvider::class,
    Illuminate\Auth\AuthServiceProvider::class,
    \App\Providers\AuthServiceProvider::class,
],

config/swoole_websocket.php

'middleware' => [
     SwooleTW\Http\Websocket\Middleware\DecryptCookies::class,
    SwooleTW\Http\Websocket\Middleware\StartSession::class,
    SwooleTW\Http\Websocket\Middleware\Authenticate::class,
],

Socket.io-Client v3

this.socket = io('http://localhost:80',
  {
    transports: ['websocket'],
    withCredentials: true,
  }
);

routes/websocket.php

Websocket::on('connect', function ($websocket, $request) {
  var_dump("Trying to Access Authenticated User!");
  $user = $request->user();
  var_dump( $user );
  var_dump( auth()->user() );
  }

I am also using Laravel Sanctum.

  1. What did you expect to see? The authenticated user object

  2. What did you see instead?

    Null

    How can i achieve, getting the correct user?

michabbb commented 2 years ago

interesting, after reading the wiki and finding out that there is a middleware for sockets to authenticate a user, I have no clue how to authenticate a user while establishing a socket connection. I would be happy if the author could show us an example that demonstrates what the client has to do.

evecimar commented 2 years ago

I have same problem