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 #443

Closed Gemui closed 4 years ago

Gemui commented 4 years ago
  1. Environment Versions

    Software Version
    PHP 7.4
    Swoole 4.5.5
    Laravel 7
  2. Websocket Auth Problem Case

    laravel-swoole have problem in authentication with laravel 7 i tested it in laravel 5.8 it working perfect but in version 7 user return null all time with any guard default or other as you can see image

that trigger when i try to connect from socket io

let socket = io('ws://127.0.0.1:1215', {transports: ['websocket']})

swoole_http.php providers

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

i tried to change in package Authenticate but i got not thing

it's a bug in necessary feature i hope we found solution in fast time

Arkanius commented 4 years ago

I see, I'll try to work on a fix to it, but, unfortunatelly I don't have write access on this repo, so, in any case we'll need to wait the mainteiners (@albertcht ) to merge it and it could take a little longer than you expect

albertcht commented 4 years ago

Hi @Gemui ,

In laravel 5.x, basically developers can use built-in authentication in websocket out of the box. There might be some changes in newer versions of Laravel.

You can try to organize your own middleware in swoole_websocket.php, and you can customize your auth middleware and replace it.

SchahinRohani commented 3 years ago
Software Version
PHP 8.0.8
Swoole 4.7.0
Laravel 8.51.0
laravel-swoole 2.8

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 have the same problem with authenticating trough guards and getting the user on:('connect'). The request and the guard always return NULL instead of the user.
I've tried to use the web the midleware from the documentation and also the web middleware. I am using Sanctum.
I am authenticating the user in a vuejs spa via post request to a route, getting a session cookie, then connecting with socket.io-client v3 to the websocket server of swoole. i am using the socket.io-client withHeader: true option, so its passing the cookies with the request to the websocket server. So then on:('connect'), i want to know the user, which is connecting by using the guard. But its always returning null.

How can i achieve, getting the correct user?