0
votes

I dont understand why I get this issue when trying to connect to private channel. I have 100% identical code to this post - Unable to authenticate laravel private channel using Laravel echo server, redis and socket.io though with the redis prefix does not work... It works with simple channel... Here is my error -

Client can not be authenticated, got HTTP status 403 with laravel-echo-server

Here is my code js-

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001',
    auth: {
    headers:
        {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    }
});
window.Echo.private('user.'+userIdentifier).listen('SomeTestEvent', function (e) {
...
} 

routes/channels.php -

Broadcast::channel('user.{id}', function ($user, $id) {
    return true; // returning always true just for test
});

BroadcastServiceProvider.php -

public function boot()
    {
        Broadcast::routes();

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

Events/SomeTestEvent.php -

public function broadcastOn()
    {
        return new PrivateChannel('user.'.$this->data['user_id']);
    }

laravel-echo-server.json -

{
    "authHost": "http://localhost:8000",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "e0ebecd04673b905",
            "key": "39c2a1314e4ef8e2879486a4a3b91c1e"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {
            "host": "localhost",
            "port": "6379"
        },
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": "",
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "secureOptions": 67108864,
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "subscribers": {
        "http": true,
        "redis": true
    },
    "apiOriginAllow": {
        "allowCors": false,
        "allowOrigin": "*",
        "allowMethods": "GET, POST",
        "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
    }
}

1

1 Answers

0
votes

Oh gosh I just find out how to do that.

I needed to use - Broadcast::routes([ 'middleware' => 'auth:api' ]);

And I added the authorization token in frontend -

 auth: {
    headers:
    {
        'Authorization': 'Bearer ' + accToken,
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
}