2
votes

Unable to authenticate users in my chat app. I am getting a 403 error from the console. This happens though when I use private channels, but when using a public channel, this is working really fine but I definitely want authenticated users only. It is more like an spa, hence using axios for almost everything including user authentication requests to laravel. below is my code:

BroadcastServiceProvider:

` public function boot()
{
    Broadcast::routes();

    require base_path('routes/channels.php');
}`

Channels.php:

`Broadcast::channel('App.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

Broadcast::channel('chat', function ($user) {
    return Auth::check();
});
`

listen directive from vue component:

`Echo.private('chat')
            .listen('.App\\Events\\Chats\\MessageSent', (e) => {
                console.log(e);
                this.sentMessages.push({
                    message: e.message.message,
                    user: e.user
                });
`

MessageSent event:

`   public function broadcastOn()
{
    return new PrivateChannel('chat');
}
`

Now using the vue-echo wrapper but still I got this problem, I still haven't figured out what I am missing

1
are you using laravel-echo-server with redis ?Djellal Mohamed Aniss
using laravel echo with pusherJohhn

1 Answers

0
votes

It's is just basically as the error suggests, an authentication problem, well I am using tokens for authentication in my app but now needed to also pass this token issued to the user as well to vue-echo.

And also change:

Broadcast::routes();

to

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

since am creating a single page application hence using axios for authentication which therefore interprets to me using the api middleware.

Guided by the answer provided by Alex on the question 'Laravel /broadcasting/auth Always Fails With 403 Error ' You can get more details there.

Thanks all .