Search code examples
laravelauthenticationchatbroadcast

POST http://localhost:8000/broadcasting/auth 403 (Forbidden) / for customer service chat where i only need auth for admins


How can I use broadcasting/auth for the admin but not for the normal user, and still both can chat with each other?

// BroadcastServiceProvider

public function boot()
{

    Broadcast::routes();

    require base_path('routes/channels.php');
}

// Channels.php

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

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

// MessageSent Event

public function broadcastOn()
{
    return new PresenceChannel('chat');
}

I'm getting this error: POST http://localhost:8000/broadcasting/auth 403 (Forbidden), but when i remove from laravel echo (Echo.join('chat')) the error goes.


Solution

  • You can achieve such thing using Middleware in laravel.

    This quote is from laravel's official website:

    Middleware provide a convenient mechanism for filtering HTTP requests entering your application. For example, Laravel includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application.

    Complete reading from this website