laravel-swoole: Still getting the last authenticated user for every request. Something wrong with sandbox mode?

  1. Please provide your PHP and Swoole version. (php -v and php --ri swoole)

Swoole 2.1.3 on PHP 7.2.5

  1. Please provide your Laravel/Lumen version.

Laravel 5.6.20

  1. What did you do? If possible, provide a recipe for reproducing the error.

Log in to the application via Chrome. Ensure you are logged in. Open up another browser (f.e. Firefox) and visit the application’s URL. I am instantly logged in in the Firefox session.

  1. What did you expect to see?

Not being logged in in the Firefox session.

  1. What did you see instead?

Logged in in the Firefox session with the user I logged in with in the Chrome session.


I have enabled sandbox mode and running laravel-swoole behind nginx. For some reason, auth isn’t reset after each request. Any ideas?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 36 (15 by maintainers)

Most upvoted comments

But I’d also like to express my thanks for developing this amazing plugin. My API became 3 times faster on average, without me having to do any changes whatsoever. As far as I’m concerned it’s basically magic. Thanks again!

Hi @lbadger ,

Try to add Laravel\Passport\PassportServiceProvider::class to providers in config/swoole_http.php, not sure if it helps thought.

And more details are needed to clarify your issue. You can refer to Issues-Guideline and Debug Guideline and open another issue.

my issue solved after updating config to

/* |-------------------------------------------------------------------------- | Instances here will be cleared on every request. |-------------------------------------------------------------------------- */ ‘instances’ => [ ‘auth’, ],

The fastest way I can help you is a laravel repo which can reproduce your bug. Welcome to open another issue if you find something new.

Thanks all for trying to debug this issue. I haven’t found the culprint yet. I will report back in a new issue if I find something useful.

Hi everryone,

I decide to close this issue first because:

  1. More and more people commented here to report their issue. However, it will make it difficult to track the original issue.
  2. Some people might think their issues are same or related to this one, but they are caused by different factors (middleware, 3rd packages, etc.).

Thanks a lot for all the comments in this issue, and some debug tips are collected to Debug Guideline.

So please open a new issue that it will be easier for a new discussion.

Hi @LostNCG ,

Thanks for your report. The behavior in websocket connect might be different from Laravel HTTP. The request passing to connect event callback is processed by my own implementaion. It’s implemented with pipeline as well though, but they are separate.

Since getting an auth user depends on request and session by default in Laravel, however, there’s no request in these event callbacks. But you inspire me maybe I can design another API for getting an auth user in these event callbacks by sender’s socket id.

The websocket will handshke with the server with HTTP protocol first, then upgrade to websocket protocol. That means request will only be available in connect callback.

Anyway, thanks for your report, it helps!

This reproduce steps may as your reference:

websocket.php

Websocket::on('connect', function ($websocket, $request) {
    dump("In connect event: ");
    dump('$request->user() return '.($request->user() ? $request->user()->getName() : 'guest'));
    dump('auth()->user() return '.(auth()->user() ? auth()->user()->getName() : 'guest'));
    dump(auth()->check() ? 'auth()->check() return true' : 'auth()->check() return false');
    dump("----------------------------------------------------------------------------------");
});

Websocket::on('subscribe', function ($websocket, $data) {
    dump("In subscribe event: ");
    dump('auth()->user() return '.(auth()->user() ? auth()->user()->getName() : 'guest'));
    dump(auth()->check() ? 'auth()->check() return true' : 'auth()->check() return false');
    dump("----------------------------------------------------------------------------------");
});

Case 1: without clear auth instance in swoole_http.php

Result as expected when:

  1. user not logged in
  2. logged in

Unexpected result when:

  1. after user logged out

Case 2: with clear auth instance in swoole_http.php

Result as expected when:

  1. user not logged in
  2. logged in

Unexpected result when:

  1. after user logged out

But outcome slightly different with case 1

"----------------------------------------------------------------------------------"
"In connect event: "
"$request->user() return guest"
"auth()->user() return guest"
"auth()->check() return false"
"----------------------------------------------------------------------------------"
"In subscribe event: "
"auth()->user() return user details"
"auth()->check() return true"
"----------------------------------------------------------------------------------"

After set auth instance in config, everything work fine until I try to check user status in custom event “subscribe”.

Hope this report can help you in debug.

Hi @albertcht - I might have a look next week to see if I can make a copy of my (fairly big) app and throw everything out until I have a small reproduction of the problem. Hope it won’t take me too long…

Hi, since @deepakSP 's setup seems to work now, I will try your suggested setup and report back. Might be tomorrow though.