laravel-echo-module icon indicating copy to clipboard operation
laravel-echo-module copied to clipboard

Refreshed tokens not applied

Open iranagame opened this issue 4 years ago • 1 comments

If socket has already connected through a valid token and token gets refresh after a while, the library still using the old token and that will cause 401 subscription_error on subscribe requests! I'm experiencing this on socket.io adapter.

Any ideas on how solve this?

iranagame avatar Apr 28 '21 09:04 iranagame

I experience the same issue due to working with nuxt-auth + laravelJWT refresh scheme. So dirty workaround for now it that I set actual access_token by settings actuall token on

mounted(){
  this.$echo.options.auth.headers['Authorization'] = this.$auth.strategy.token.get()
... // logic to connect by $echo to channel
}

or better one I'm using right now:

plugin: axios.js

export default function ({ app, $axios }) {
  $axios.onRequest(
        async request => {
             try {
                if (app.$auth.strategy.token.status().expired()) {
                    await app.$auth.refreshTokens()
                }
                // set the Authorization header using the access token fetched from login config
                const actualAuthToken = app.$auth.strategy.token.get()
                request.headers['Authorization'] = actualAuthToken
                app.$echo.options.auth.headers['Authorization'] =
                    actualAuthToken

                app.$auth.strategy.token.sync()
            } catch (e) {
                console.error(e)
            }

            return request
        },
        error => {
            return Promise.reject(error)
        }
    )
}

have found better fix?

devzom avatar Sep 01 '21 08:09 devzom