How to define or pass auth guard for broadcast authentication routes instead of default auth guard?

北战南征 提交于 2019-12-10 19:26:15

问题


I am very new to realtime event broadcasting, I have simple laravel-echo-server setup and working with everything. I am unable to set/define authentication against other auth guard it is always checking with user/default guard defined in auth.php I have setup the authentication routes for each guards private channels in routes/channel.php as below per documentation.

For auth guard user private channels

Broadcast::channel('users.{id}', function ($user, $id) {
   Log::info(class_basename($user));
   return (int) $user->id === (int) $id;
});

For auth guard admin private channels

Broadcast::channel('admins.{id}', function ($admin, $id) {
   Log::info(class_basename($admin));
   return (int) $admin->id === (int) $id;
});

It works fine for guard user that is the first case but never worked for the second one i.e. admin guard. and the

Log::info(class_basename($admin)) always returns User class.

So, how do we pass or define that it should use admin guard instead of user. after exploring the inside of Illuminate\Broadcasting\Broadcasters\Broadcaster I found out that below in line 411

public function user($guard = null)
{
    return call_user_func($this->getUserResolver(), $guard);
}

So, if we can pass this guard parameter it can solve the purpose. If anyone can give me anything or way of authorising with multiple guard setup that will be very helpfull. Using Laravel 5.4, laravel-echo-server, Redis, Socket.IO


回答1:


Just like in the otherwhere, simply use Request facade in the closure.

In your case:

Broadcast::channel('admins.{id}', function ($user, int $id) {
   return Request::user('admin')->id === $id;
});

The arguments sent to the closure can not be change by user, it is controlled by laravel framework. (see BroadcastManage and RedisBroadcaster, or other implementations of Illumiante\Contracts\Broadcasting\Broadcaster)



来源:https://stackoverflow.com/questions/43682681/how-to-define-or-pass-auth-guard-for-broadcast-authentication-routes-instead-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!