Laravel /broadcasting/auth Always Fails With 403 Error

后端 未结 8 2116
时光取名叫无心
时光取名叫无心 2021-01-04 03:58

I have recently delved into Laravel 5.3\'s Laravel-Echo and Pusher combination. I have successfully set up public channels and moved on to private ones. I am having trouble

相关标签:
8条回答
  • 2021-01-04 04:29

    In case, someone comes in the latest Laravel 5.7 with the same issues, the good solution worked for me is to check authentication in each channel before returning or on the return like below.

    Broadcast::channel('user.*', function ($user) {
        return Auth::check();
    });
    
    Broadcast::channel('conversation.{id}', function ($user, $conversationId) {
        Auth::check();
        return $user->isInConversation(Conversation::find($conversationId));
    });
    

    This way it works with any channel broadcasting any event including users.

    I hope it may help someone else.

    0 讨论(0)
  • 2021-01-04 04:31

    I paired socket.io with redis and also had a problem with 403 error, even though there weren't any authentication middlewares over /broadcasting/auth route. Only after whatching laracasts lesson I figured out that just channel authorization is not enough, there always should be user and no matter how you authenticate and obtain user, using default laravel auth or some token algorithm - jwt or anything else.

    Authenticated user is automatically resolved and passed as first parameter to to closures functions in routes/channels.php file, so you can check channel availability for currently logged in user

    0 讨论(0)
提交回复
热议问题