Laravel Echo Client cannot be authenticated

你离开我真会死。 提交于 2021-01-28 06:34:52

问题


I see this question has been asked multiple times, but non of the answers are working so I am asking for help and some insight with it.

I am using laravel echo and redis for broadcasting events. My laravel echo server stars up nicely, redis is running nicely.

When I reload my webpage I get this error in the laravel-echo server terminal:

⚠ [4:49:19 PM] - qg4WSWl5YusQY8hJAAAA could not be authenticated to private-test-channel.1
{"error":"Unauthenticated."}
Client can not be authenticated, got HTTP status 401

and I have gone back and forth with that to do with it and nothings helps with it.

Here is my app.js

import Echo from "laravel-echo";

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

Here is my echo.js

  window.Echo.private('test-channel.{{Auth::user()->id}}')
    .listen('EventName', function(event) {
      console.log('data', event);
  });

My BroadcastServiceProvider

public function boot()
{
  //broadcast routes middleware
  Broadcast::routes(['middleware' => ['auth', 'web']]);

  /*
  * Authenticate the user's personal channel...
  */
  Broadcast::channel('App.User.*', function ($user, $userId) {
    return (int) $user->id === (int) $userId;
  });

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

this is my larave-echo-server.json

{
    "authHost": "http://127.0.0.1:8000",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "570453f474365cc8",
            "key": "4baec74e662696009e7857e49d3364c4"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": "127.0.0.1",
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": ""
}

When I fire my event, then I get the event in the console, but the user cannot be authenticated for some reason and I cannot figure it out.


回答1:


Have you seen in routes/channels.php for permission?

this is the link for explanation: https://laravel.com/docs/5.5/broadcasting#authorizing-channels I see that you have put BroadcastServiceProvider

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

But in routes/channels.php default command is:

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

maybe it is creating some conflict or problem



来源:https://stackoverflow.com/questions/46490400/laravel-echo-client-cannot-be-authenticated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!