Skip to content

0.5.1: simplify auth - make the "bearerToken" option work, and handle websocket disconnects

Compare
Choose a tag to compare
@leob leob released this 16 Nov 11:12
· 26 commits to master since this release

Two changes:

1- Handle websocket reconnects
2- Fix "auth"/"bearerToken" options for private channels authentication

ad 1) See #2

ad 2) Explanation:

To implement authorization for private channels, we previously had to use a "hack"/workaround like this:

window.axios.defaults.headers.common['Authorization'] = 'Bearer ' + token;

window.Echo = new Echo({
    broadcaster,
    host,
    enabledTransports: ["ws", "wss"],
    authEndpoint,
});

This was because the auth option of the Echo object didn't work, so we just passed the bearer token via an Axios header (or equivalent).

With this PR, we can now do this instead:

window.Echo = new Echo({
    broadcaster,
    host,
    enabledTransports: ["ws", "wss"],
    authEndpoint,    
    bearerToken: token,
});

P.S. the above is a simplified syntax for the following:

window.Echo = new Echo({
    broadcaster,
    host,
    enabledTransports: ["ws", "wss"],
    authEndpoint,
    auth: {
        headers: {
            Authorization: 'Bearer ' + token,
        }
    },
});

as per: laravel/echo#353