nuxt-community / laravel-echo-module

Laravel Echo for Nuxt 2
MIT License
85 stars 32 forks source link

Access to echo instance on auth scheme #37

Open vbaseghyanupwork opened 3 years ago

vbaseghyanupwork commented 3 years ago

Hi there,

I have this in my nuxt.config.js

buildModules: [
        // Doc: https://github.com/nuxt-community/eslint-module
        '@nuxtjs/eslint-module',

        // Doc: https://github.com/nuxt-community/nuxt-tailwindcss
        '@nuxtjs/tailwindcss',
        '@nuxtjs/laravel-echo',
        // Doc: https://github.com/nuxt-community/dotenv-module
        ['@nuxtjs/dotenv'],
 ],

and outside of build module

    echo: {
        authModule: true,
        connectOnLogin: true,
        disconnectOnLogout: true,
        broadcaster: 'pusher',
        key: process.env.PUSHER_KEY,
        authEndpoint: process.env.SOCKET_HOST + '/broadcasting/auth',
        cluster: 'eu',
        plugins: ['~/plugins/echo.js'],
    },

and auth scheme like

    auth: {
        redirect: {
            login: '/login',
            logout: '/login',
            callback: false,
            home: false,
        },
        strategies: {
            myScheme: {
                _scheme: '~/schemes/myScheme',
                endpoints: {
                    login: {
                        url: process.env.API_URL + '/login-end-oint',
                        method: 'post',
                        propertyName: 'access_token',
                    },
                    logout: {
                        url: process.env.API_URL + '/logout',
                        method: 'delete',
                    },
                    user: {
                        url:
                            process.env.API_URL +
                            '/user/profile',
                        method: 'get',
                        propertyName: 'data',
                    },
                },
            },
        },
    },

and my echo.js in plugins

export default function ({ $echo, app }) {

    if (
        app.$auth &&
        app.$auth.$state.hasOwnProperty('user') &&
        app.$auth.$state.user &&
        app.$auth.$state.user.hasOwnProperty('id')
    ) {
        const id= app.$auth.$state.user.id
        $echo
            .private('my-notifications.' + id)
            .listen('.TestEvent', (e) => {
                console.log('event triggered')
                console.log(e.entity)
            })
    }
}

So the issue here, is when I'm logging in, this doesn't make connection to that channel automatically, and works only when I'm doing refresh in browser.

So basically authorization is not happening when I'm logging in.

Any hints, how can I do that?

Thanks

ricardogobbosouza commented 3 years ago

Hi @vbaseghyanupwork Sorry for the late reply I will think about having an option to be called before and after the echo connects

For now you can try this:

export default function ({ $echo, app }) {
  ctx.app.$auth.$storage.watchState('loggedIn', loggedIn => {
    if (loggedIn) {
      const id= app.$auth.$state.user.id
      $echo.private('my-notifications.' + id).listen('.TestEvent', (e) => {
        console.log('event triggered')
        console.log(e.entity)
      })
    }
  })
}