Laravel /broadcasting/auth Always Fails With 403 Error

后端 未结 8 2115
时光取名叫无心
时光取名叫无心 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:10

    I solve it by creating channel route.

    Create your Authorizing Channels in routes->channels.php

    Broadcast::channel('chatroom', function ($user) {
        return $user;
    });
    

    See Documentation : https://laravel.com/docs/5.4/broadcasting#authorizing-channels

    thanks

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

    What worked for me was to use the method private of the Laravel Echo package: https://laravel.com/docs/5.3/notifications#listening-for-notifications

    Echo.private('App.User.1')
      .notification((notification) => {
      console.log(notification.type);
    });
    
    0 讨论(0)
  • 2021-01-04 04:14

    In my case the problem was a wrong user id:

    Echo.private('user.'+CURRENT_USER_ID_HERE)
    
    0 讨论(0)
  • 2021-01-04 04:15

    Error 403 /broadcasting/auth with Laravel version > 5.3 & Pusher, you need change your code in resources/assets/js/bootstrap.js with

    window.Echo = new Echo({
        broadcaster: 'pusher',
        key: 'your key',
        cluster: 'your cluster',
        encrypted: true,
        auth: {
            headers: {
                Authorization: 'Bearer ' + YourTokenLogin
            },
        },
    });
    

    And in app/Providers/BroadcastServiceProvider.php change by

    Broadcast::routes()
    

    with

    Broadcast::routes(['middleware' => ['auth:api']]);
    

    or

    Broadcast::routes(['middleware' => ['jwt.auth']]); //if you use JWT
    

    it worked for me, and hope it help you.

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

    This can happen if you are no longer logged in. Make sure you are actually logged into the Laravel app and that your current session hasn't expired.

    I logged back in and it worked for me.

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

    Check how you are authorising your channel. Depending on your setup this might help. Update your BroadcastServiceProvider with the following:

    <?php
    
    namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    use Illuminate\Support\Facades\Broadcast;
    
    class BroadcastServiceProvider extends ServiceProvider
    {
        /**
         * Bootstrap any application services.
         *
         * @return void
         */
        public function boot()
        {
            Broadcast::routes(['middleware' => ['auth:api']]);
    
            require base_path('routes/channels.php');
        }
    }
    

    Adds in the Auth API middleware for use with Laravel Passport.

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