Support the ongoing development of Laravel.io →
Authentication API Laravel

hi, im using Laravel 8 as api backend and react js in front.

im trying to build a websocket connection with react and laravel using laravel-echo-server, laravel-echo and redis.

the problem is im not using laravel guard auth ( the auth process is written by my self for some reasons and handled with middlewares not auth:api guard ) and so i can't using laravel guard for authenticate the user in private channels. beacuse of that im looking for other ways to authenticate users in private channels without laravel guard. i tried laravel-echo authorizer :

window.io = socketio;
window.Echo = new Echo({
       authorizer: (channel, options) => {
             return {
                    authorize: (socketId, callback) => {
                          axios.post('/api/auth/custom', {
                               socket_id: socketId,
                               channel_name: channel.name
                           }).then(response => {
                               callback(false, response.data);
                           }).catch(error => {
                               callback(true, error);
                     });
                }
           };
      },
      broadcaster: 'socket.io',
      host: `${host}:${port}`,
      transports: ['websocket'],
});

but nothing happend. actually no request is sent to the '/api/auth/custom'

at the end, Channels are working but Private Channels need authentication...

any solutions ?

Last updated by @mora 3 years ago.
0

it seems that the header in echo is missing

Echo.connector.pusher.config.auth.headers['Authorization'] = 'Bearer token';

You can check below link for further details. https://github.com/tlaverdure/laravel-echo-server/issues/129#issuecomment-284216291

0

@skynet-technologies i dont use pusher and problem is no request is sent to the '/api/auth/custom', with header or not

0

The above code was an example which means, we have to pass a header with authentication(using bearer) You can check the below code and implement your code within it.

window.Echo = new Echo({
            broadcaster: 'socket.io',
            host: HOST_FOR_YOUR_SERVER,
            auth: {
                headers: {
                    Authorization: 'Bearer ' + AUTH_API_TOKEN,
                },
            },
        });

BroadcastServiceProvider:

Broadcast::routes(['middleware' => ['auth:api']]);

For more reference, you can check the below URL https://github.com/laravel/framework/issues/23268#issuecomment-443986055

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Mora mora Joined 22 Jun 2022

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.