laravel broadcasting for multiple guard

后端 未结 2 746
南方客
南方客 2021-02-06 17:40

I have the below auth guards that is defined for my app admins, designers, customers and etc. the default guard is the designer guar

2条回答
  •  长发绾君心
    2021-02-06 18:16

    I am Posting this answer, For every one who might face the problem no-days. I am using laravel 7 and beyondcode/laravel-websockets. As I dig in the source code specifying midllewares in BoradcastServiceProvider.php will not work. The only way to define a guard for a channel is by specifying options for the channel:

     Broadcast::channel('messaging.organ.{id}', function ($organ , $id) {
        return $organ->id == $id && get_class($organ) === "App\Organization";
    } , ['guards' => ['organ']]);
    

    Reason: because I am using beyondcode/laravel-websockets so I dig in src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php and in this file the retrieveUser method will get the user. in this file if an option for the channel is provided the user in the specified guards will be returned. you can define one or multiple guards, however it will only returns one user who is logged in as a guard that comes first in the array.

        protected function retrieveUser($request, $channel)
    {
        $options = $this->retrieveChannelOptions($channel);
    
        $guards = $options['guards'] ?? null;
    
        if (is_null($guards)) {
            return $request->user();
        }
    
        foreach (Arr::wrap($guards) as $guard) {
            if ($user = $request->user($guard)) {
                return $user;
            }
        }
    }
    

提交回复
热议问题