Laravel /broadcasting/auth Always Fails With 403 Error
Solution 1
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.
Solution 2
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
Solution 3
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
Solution 4
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.
Solution 5
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);
});
LorienDarenya
Updated on September 04, 2021Comments
-
LorienDarenya over 2 years
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 with Laravel returning a 403 from the /broadcasting/auth route, no matter what I do to try to authorize the action (up to and including using a simple return true statement). Can anyone tell me what I am doing wrong?
App/Providers/BroadcastServiceProvider.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(); /* * Authenticate the user's personal channel... */ Broadcast::channel('App.User.*', function ($user, $userId) { return true; }); } }
resources/assets/js/booststrap.js:
import Echo from "laravel-echo" window.Echo = new Echo({ broadcaster: 'pusher', key: 'My-Key-Here' }); window.Echo.private('App.User.1') .notification((notification) => { console.log(notification.type); });
I can see the event and it's payload in my Pusher debug console, it is simply failing once it hits the auth route.