Laravel Echo authentication via API

2.1k Views Asked by At

Using Pusher in conjunction with Echo on the client, I'm attempting to authenticate a private channel via api using Bearer Authorization header token - however I'm met with the following logs (THE LAST LOG BEING THE ERROR):

Pusher : : ["State changed","initialized -> connecting"]

Pusher : : ["Connecting",{"transport":"ws","url":"ws://ws-mt1.pusher.com:80/app/************?protocol=7&client=js&version=7.0.3&flash=false"}]

Pusher : : ["State changed","connecting -> connected with new socket ID 224550.32180982"]

Pusher : : ["No callbacks on private-App.User.17 for pusher:subscription_error"]

Pusher : : ["Error: JSON returned from auth endpoint was invalid, yet status code was 200. Data was: <!doctype html>\n<html class="en" lang="en">\n\t\n\t\t\t<meta charset="utf-...

Seems like my server is just returning the webpage rather than a JSON response.

Here is my Client setup:

Pusher.logToConsole = true

const PusherClient = new Pusher(PUSHER_APP_KEY,{
    cluster: PUSHER_APP_CLUSTER,
    wsHost: `ws-${PUSHER_APP_CLUSTER}.pusher.com`,
    wssPort: 443,
    enabledTransports: ['ws'],
    forceTLS: true,
    encrypted: true,
    auth: {
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${token}`
        }
    },
    authEndpoint: http://127.0.0.1:8000/api/broadcasting/auth'
});

return new Echo({
    broadcaster: 'pusher',
    client: PusherClient
})

Broadcast routes defined in api.php on Laravel server:

Route::group(['middleware' => 'auth:api'], function () {
    Broadcast::routes();
});

enter image description here

What am I missing here?

1

There are 1 best solutions below

0
On

First you can setup client side without laravel-echo, just with pusher-js. In the version I'm using there shouldn't be Content-Type header:

    auth: {
        headers: {
            'Authorization': `Bearer CLIENT_TOKEN`,
        }

    },

You must make sure the channel you're subscribing is available and all pusher app parameters and also user's token are inserted correctly.