mercredi 17 janvier 2018

Laravel authorization for Broadcast channels not working

I am trying to integrate chat into a webapp using Laravel 5.4 (backend) and Angular 2 (frontend). I have a unqiue Broadcast channel that emits a message to a user. Each user has their own channel which is chat-{uuid} with uuid being a unique ID in a chat table which also stores the userID of the user who can access that channel. The problem is that the Authorization callback is never called so any user can access the channel if they know the uuid. I'm not sure what I'm doing wrong

channels.php

// This authorization is never called 
Broadcast::channel('chat-{uuid}', function ($user, $uuid) {
    return false; 
});

NewMessage.php event

protected     $message;

public function __construct($message)
{
    $this->message = $message;
}

protected function prepareData()
{
    return [
        'chatID'                => $this->message->chatID,
        'userID'                => $this->message->builderID,
        'message'               => $this->message->message,
        ];

}

public function broadcastWith()
{
    return [
        'message' => $this->prepareData(),
    ];
}

public function broadcastAs()
{
    return 'new.message';
}

public function broadcastOn()
{
    return new PrivateChannel('chat-'.$this->message->chatID);
}

BroadcastServiceProvider.php

public function boot()
{
    Broadcast::routes( [ 'middleware' => [ 'api', 'auth.jwt' ] ] );
    require base_path('routes/channels.php');
}

Connecting to the broadchannel on the frontend (Messages are being recieved)

Component.ts

  window['Echo'] = new Echo({
    broadcaster: 'socket.io',
    host: 'http://app.test:6001',
    auth:
      {
        headers:
          {
            'Authorization': 'Bearer ' + this.auth.jwt
          }
      }
  });

window['Echo'].private(`chat-${this.chatUUID}`)
  .listen(".new.message", (data) => {
    this.messages.data.push(data.message);
  });



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2rjRUsU
via IFTTT

Aucun commentaire:

Enregistrer un commentaire