vue-keycloak-js icon indicating copy to clipboard operation
vue-keycloak-js copied to clipboard

$keycloak not available in context in Vue 2 + Composition API

Open vladninja opened this issue 3 years ago • 5 comments

Hi, I am using this amazing plugin with Vue 2 + Composition API. The problem is - $keycloak not available in context. In setup(props, context), in context.root there is no information about $keycloak.

The only one way to solve this is on creating a Vue instance, create a watcher, which "sync" results from the $keycloak to my store and then, I will have an information in the setup, but it's overkill.

What I do: this.$watch("$keycloak.token", setToken, { immediate: true });

Can you help me with this? Could you add a $keycloak to the context for the Vue 2 + Composition API? Thanks

vladninja avatar Jul 16 '21 09:07 vladninja

We could probably make use of provide/inject so you could get it in the setup method.

As a workaround you could probably also use getCurrentInstance https://v3.vuejs.org/api/composition-api.html#getcurrentinstance although not recommended.

baltom avatar Jul 20 '21 10:07 baltom

Could you then get ahead with this provide/inject proper way?

idc77 avatar Jan 10 '22 17:01 idc77

@vladninja @idc77 maybe, you can add like this

const keycloak = new Keycloak(initOptions) .... keycloak.init({ onLoad: 'check-sso' }).success((auth) => { ....... new Vue({ .... keycloak, .... render: (h) => h(App), }).$mount('#app') ..... }).error(() => { Vue.$log.error('Authenticated Failed') })

and you can get data from context, like this

console.log(ctx.root.$options.keycloak)

ugiispoyo avatar Jun 15 '22 02:06 ugiispoyo

This is the plug-in I used in the VUe2 + Compostion API. I don't know if it's what you want! image

image

@vladninja

cyccess avatar Jun 17 '22 02:06 cyccess

Could you then get ahead with this provide/inject proper way?

any update on this ?

Ribeiro-Tiago avatar Dec 05 '23 15:12 Ribeiro-Tiago