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 ?
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
@skynet-technologies i dont use pusher and problem is no request is sent to the '/api/auth/custom', with header or not
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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community