问题
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