laravelbroadcastingsanctum

broadcasting auth endpoint returns HTML response instead of JSON


I upgraded my laravel 8 backend for SPA (vue.js, sanctum) to laravel 11, and post request /broadcasting/auth returns HTML response instead of json like { auth: "..." }

In previous version Broadcast::routes has been called from BroadcastServiceProvider, but now it is under the hood of laravel framework in ApplicationBuilder.php, and previously I've added sanctum middleware like this Broadcast::routes(['middleware' => ['auth:sanctum']]);

How should I do it in laravel 11?


Solution

  • I 've fixed broadcasting/auth by commenting out channels:... in app.php and putting in AppServiceProvider this

        Broadcast::routes(['middleware' => ['auth:sanctum']]);
        
        Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
            return (int) $user->id === (int) $id;
        });
        Broadcast::channel('user.{toUserId}', function ($user, $toUserId) {
            return $user->id == $toUserId;
        });