vue-keycloak-js
vue-keycloak-js copied to clipboard
$keycloak not available in context in Vue 2 + Composition API
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
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.
Could you then get ahead with this provide/inject proper way?
@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)
This is the plug-in I used in the VUe2 + Compostion API.
I don't know if it's what you want!
@vladninja
Could you then get ahead with this provide/inject proper way?
any update on this ?