happyDemon / vue-echo

Vue integration for the Laravel Echo library.
228 stars 24 forks source link

Erro 401 - laravel + broadcasting + socket.io + redis with passport #17

Closed renatosistemasvc closed 6 years ago

renatosistemasvc commented 6 years ago

public channels function normally. For private channels, the error is:

⚠ [04:53:17] - 4JvRYXBHYYCJCB1UAAAD could not be authenticated to private-notification.1 {"message":"Unauthenticated."} Client can not be authenticated, got HTTP status 401

I tried every possible possibility and I did not succeed!I'm desperate because I have time to deliver this functionality. Can someone help me? Why can not I authenticate private channels with passport?

My current setting:

BroadcastServiceProvider.php

ublic function boot()
{

    Broadcast::routes(['middleware' => 'auth:api']);
    require base_path('routes/channels.php');
}

laravel-echo-server.json

{
    "authHost": "http://www.softshoplocal.com.br",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "9c93c4f229bb615f",
            "key": "6efe137de4101bc1b0429e6afd29e817"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": "www.softshoplocal.com.br",
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": ""
}

channels.php

Broadcast::channel('notification.1', function ($user, $lojaId) {
    return true;
});

SendNotification.php

public function broadcastOn()
{
        //return new Channel('notification');
        return new PrivateChannel('notification.1');
}

my frontend (file vue)

Vue.use(VueEcho, {
    broadcaster: 'socket.io',
    host: 'localhost:6001',
    auth: {
        headers: {
            Authorization: 'Bearer' + sessionStorage.getItem("ss348978Token")
        },
    },
});

.....

this.$echo.private('notification.1').listen('SendNotification', (payload) => {
            this.mensagem.push(payload.data)
});
renatosistemasvc commented 6 years ago

after a week trying to solve, I finally managed to solve. The problem was in this code

I changed from

Broadcast::channel('notification.1', function ($user, $lojaId) {
    return true;
});
for
Broadcast::channel('notification.{notification}', function ($user, $lojaId) {
     return true; 
});

I'm sorry for bothering you. I thought it was a bug, but it's not.