Skip to content

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

@mrmmg

Description

@mrmmg

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions