nuxt-community / laravel-echo-module

Laravel Echo for Nuxt 2
MIT License
86 stars 31 forks source link

Refreshed tokens not applied #53

Open iranagame opened 3 years ago

iranagame commented 3 years ago

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?

devzom commented 2 years ago

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?