Clara Oswald
Clara Oswald

Reputation: 226

Authorization for laravel passport through websocket

I have a private channel. And i can use it only if i authenticated on site. But in case of laravel passport i have no idea how it should be work. By default all what i need contains in cookies. What i have:

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

Broadcast::routes(['middleware' => ['api']]); have no effect.

How do I authenticate users through laravel passport, for that would be connected to a private channel websocket?

Upvotes: 2

Views: 1856

Answers (2)

hasusuf
hasusuf

Reputation: 1589

I been through this, and my solution was using Laravel Echo & Laravel Echo Server for SocketIo.

Retrieve the user access token, then store it in cookie or application storage, and inject it while you instantiating or re-instantiating Laravel Echo, like the following:

let Echo = require('laravel-echo')

window.Echo = new Echo({
        broadcaster: 'socket.io',
        host: window.location.hostname + ':6001'
        auth: {
            headers: {
                 Authorization: 'Bearer ' + AUTH_API_TOKEN,
            },
        },
    });

References:

  1. https://laravel.com/docs/5.3/broadcasting#driver-prerequisites
  2. https://github.com/tlaverdure/laravel-echo-server

Upvotes: 4

Clara Oswald
Clara Oswald

Reputation: 226

Unfortunately at the current moment the echo does not support authorization with the help of laravel passport, it is necessary to write your own sokket.io server.

Upvotes: 0

Related Questions