Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to echo instance on auth scheme #37

Open
vbaseghyanupwork opened this issue Feb 18, 2021 · 1 comment
Open

Access to echo instance on auth scheme #37

vbaseghyanupwork opened this issue Feb 18, 2021 · 1 comment

Comments

@vbaseghyanupwork
Copy link

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
Copy link
Member

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)
      })
    }
  })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants