2

In my Laravel Framework 5.7 / Vuejs 2.6 I use jwt.auth and installing pusher

I check xsfr token for my page https://imgur.com/a/CveJ6jh but I got 419 error in http://local/broadcasting/auth request and open responce of this url I see page expired message.
In app/Events/NewMessage.php :

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Message;

class NewMessage
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $message;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Message $message)
    {
        $this->message= $message;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('messages.'.$this->message->to);
    }
    public function broadcastWith()
    {
        return ['messages' => $this->message];
    }

}

In routes/channels.php :

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

But I do not see any debugging info from above.

I used pusher parameters from my prior app. I think they must be ok. Otherwise I would have different error ?

and in vue component :

mounted() {
    Echo.private(`messages.${this.customerRow.id}`)
        .listen('NewMessage', (e) => {
            this.hanleIncoming(e.message);
        });

What did I miss ?

Thanks!

2 Answers 2

6

The reason for this error is that Laravel expects a CSRF token.

You have two options:

  1. Include the CSRF token (better for security)
  2. Disable CSRF for this URL
4
  • 1
    I tried 2nd way an d got error : 403 Forbidden. I am not sure if that is correct way? Apr 24, 2019 at 14:47
  • 419 is definitely a CSRF error. The 403 must come from somewhere else. Do you have an authorization middleware that throws it?
    – Thomas
    Apr 25, 2019 at 12:54
  • As I wrote I use jwt.auth . Which authorization middleware have I to check ? Apr 25, 2019 at 15:43
  • Look at the stack trace of the 403 to see where it's thrown.
    – Thomas
    Apr 26, 2019 at 12:11
4

to solve this you need to add middleware on broadcast routes, like this.

Broadcast::routes(['middleware' => 'auth:api']);

This on the app/Providers/BroadcastServiceProvider.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.