Laravel Sanctum broke Laravel Websockets - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Monday, September 12, 2022

Laravel Sanctum broke Laravel Websockets

I think authenticating my NuxtJS SPA with the hybrid approach recommended by Laravel Sanctum broke my WebSocket connection - only the private channels. I am not 100% sure if this is the cause but here is what I have...

  • Laravel 8
  • NuxtJS
  • Laravel Websockets
  • Echo

Not so long ago I had private channels working just fine. Just recently I noticed that when I try to subscribe to a private channel I get this error (500):

Symfony\Component\Routing\Exception\RouteNotFoundException: Route [login] not defined. in file /srv/app/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php on line 444

This led me to middleware app/Http/Middleware/Authenticate.php and I see where it fails. I do not understand though why Laravel even goes to that place at all!

This is my Laravel channels code:

Broadcast::channel('container-create-{id}', function ($user, $id) {
    \Illuminate\Support\Facades\Log::info('I AM GETTING HERE!!!!');
    return true;
});

This is my NuxtJs Listener

mounted() {
    this.listenForContainerCreationAfter()
  },
  methods: {
    /**
     * Register a listener which listens for container creation after event.
     */
    listenForContainerCreationAfter() {
      console.log('trying to listen for container after creation')
      this.$echo.private('container-create-' + this.uuid)
        .listen('.container.create.after', (response) => {
          console.log(' THIS IS AFTER CONTAINER CREATION LISTENER WEBSOCKETS!')
          // after we will receive the confirmation of successful container creation we will move the user straight to
          // the dashboard where they can see more information
          this.goToDashboard()
        });
    }
  }

Other, non-private channels seem to be fine. Just private ones are not. I am quite sure the Sanctum config is fine. Especially that I did not change it since I got it to work.

When I look at the websocket package listening in the console I don't even see the subscription message.

Does anyone know what I have to change with Echo to make it work with Sanctum again?



source https://stackoverflow.com/questions/73681564/laravel-sanctum-broke-laravel-websockets

No comments:

Post a Comment