nuxt-community / laravel-echo-module

Laravel Echo for Nuxt 2
MIT License
86 stars 31 forks source link

Wrong request body: Form Data instead of JSON - private channels #57

Open robertkabat opened 3 years ago

robertkabat commented 3 years ago

Hey,

I am working on a little app where I am using NuxtJS with the laravel echo package for the frontend and Laravel8 with the LaravelWebsockets package for the backend. I am trying to make it work with private channels and passport in Laravel.

I've noticed something really strange that I can't really change or I don't know how to do that.

It looks like Laravel Echo package is making the request with FORM data instead of JSON body which I need and I have no clue if and how I could change it.

When I am trying to subscribe to a private channel(pusher as a broadcaster) echo makes a request to brodcasting/auth endpoint which is fine. However, the data in that request is sent as form data and not as JSON body.

I've made a little plugin in nuxtjs to add headers and stuff so I tried it like this:

export default function ({ $echo, $auth}) {
  console.log($echo)

  let authData = {
    headers: {
      // Authorization: 'Bearer 1c2d3a31dd0c5e1933c609ffce1ef4e3'
      Authorization: $auth.strategy.token.get(),
      // Accept: 'application/json',
      "X-Requested-With": 'XMLHttpRequest',
      "Content-Type": 'application/json'
    },
  };

  $echo.connector.pusher.config.auth = authData;
  $echo.connector._defaultOptions.auth = authData;
  $echo.connector.options.auth = authData;
  $echo.options.auth = authData;
  // $echo.options.auth = authData;
}

My headers are added (although it seems like it is hardcoded in echo package to add application/x-www-form-urlencoded header).

As a result in my Laravel I am getting 403 error and when I traced the code back to where it breaks the data from request is missing. It is because Laravel at this points expects JSON body and it is not there.

My question is - can I somehow make Echo to send the application/json body? Maybe I am misunderstanding the whole thing?

Is there some other way to make this package for Nuxt to work nicely with passport?

devzom commented 2 years ago

Dealing with the same problem atm. ;(

Jasperscheper commented 2 years ago

Did you manage to get this working?