Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

spacedog4's avatar

How to use Laravel Echo and Pusher in laravel 5.8 with API authentication

I'm trying to build a real-time chat app, but I'm having problem with laravel echo in vue js

This chat needs an api authentication, so in my broadcast it need to

this next code is in my main app.js file

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    logToConsole: true,
    encrypted: true,
    auth: {
        headers: {
            Authorization: `Bearer TOKEN`
        },
    },
    authEndpoint: "/api/broadcasting/auth",
})

How can I get this Beare Token (that I pass on the URL) and use it in my app.js file, dinamyc since It could be any user

0 likes
1 reply
bobbybouwmann's avatar
Level 88

Soo you have the correct setup here, but you only need the bearer token right?

Well that token is something you need to handle somewhere else in your frontend code. Then you can make is available for all other parts. So I suppose you have a login feature build in javascript that uses the bearer token already. If that is the case you can store the token as well

windows.access_token = response.access_token;

And then you can reuse it in your app.js file

auth: {
    headers: {
        Authorization: `Bearer ${window.access_token}`
    },
},

This is just a basic example of it. An alternative is for example storing the access token in vuex and retrieving it from there.

2 likes

Please or to participate in this conversation.