I'm stuck with this since awhile. I want to access my keycloak instance from within the vue router.js file.
const preventRoutes = {
beforeEnter: (to, from, next) => {
console.log(App.$keycloak.authenticated); //here. (Undefined)
if (store.getters.getLoginState === "true") {
...
} else {
...
}
}
}
access vue 3 app instance api from non-vue fiile
I'm trying this solution. As it's the only VUE3 I can find, but still, I think he skipped some steps. Can someone simplify please?
Adviced Solution
const preventRoutes = {
beforeRouteEnter(to, from, next) {
next(vm => {
// access to component instance via `vm`
// console.log(vm.$keycloak.authenticated);
if (this.vm.$keycloak.authenticated) {
next();
} else {
next("/");
}
})
}
}
main.js
const myApp = createApp(App)
let initOptions = {
url: KeycloakConfig["auth-server-url"], realm: KeycloakConfig.realm, clientId: KeycloakConfig.resource, clientSecret: KeycloakConfig.credentials.secret
}
const keycloak = Keycloak(initOptions)
myApp.config.globalProperties.$keycloak = keycloak;
myApp.use(VueAxios, axios)
myApp.use(store)
myApp.use(router)
myApp.mount("#app");
keycloak.init({
onLoad: "check-sso",
checkLoginIframe: false
}).then(async (auth) => {
if (!auth) {
} else if (auth) {
router.push("/dashboard");
}
}).catch((e) => {
console.log('Serwer lezy: ' + e)
})