nuxt-community / laravel-echo-module

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

How to use custom autorization without nuxt/auth module? #60

Open mrmmg opened 3 years ago

mrmmg commented 3 years ago

Hi

Thanks for your good package, I'm using laravel as backend API (with passport package) and I have some issues with your package, please help me to solve this. let me explain it to you:

I have laravel as backend in the localhost:8000 domain and have a nuxtJS project in the localhost:3000. I'm making a login request from nuxt application to laravel with Axios and save user tokens in cookies.

now when I want to subscribe to private or presence channel I get 401 and it's because of that I cannot set the header for broadcasting/auth endpoint. remember I saved user token in browser cookies.

in nuxt.config.js file i have these options for echo:

echo: {
    broadcaster: 'pusher',
    host: '127.0.0.1:6001',
    wsHost: '127.0.0.1',
    wsPort: 6001,
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: 'mt1',
    forceTLS: false,
    encrypted: false,
    disableStats: true,
    authEndpoint: 'http://127.0.0.1:8000/broadcasting/auth',
    auth: {
       headers: {
            Authorization: `Bearer ${cookies.get('user_token')}`           // i'm using js-cookie package to works with cookies
       }
    }
  },

And in Laravel:

in BroadcastServiceProvider:

 public function boot()
    {
        Broadcast::routes(['middleware' => 'auth:api']);

        require base_path('routes/channels.php');
    }

in my event file:

public function broadcastOn()
    {
        return new PresenceChannel('user.messages.chat.' . $this->user_id);
    }

in controller file:

broadcast(new ApplicationChatMessage(json_encode($message), auth('api')->id()))->toOthers();

in routes/channel.php:

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

But it's RETURN 401.

are you can help me to solve this issue?

SilvioDoMine commented 2 years ago

Same here