简体   繁体   English

在 Vue3 中使用 keycloak,但不是在启动时

[英]Using keycloak in Vue3, but not on startup

I have a problem with keycloak.js in my Vue3 application我的 Vue3 应用程序中的 keycloak.js 有问题

const keycloak = Keycloak(keycloakOptions)

keycloak.init({
  onLoad: 'login-required',
  checkLoginIframe: false
}).then(async (auth) => {
  if (!auth) {
    window.location.reload()
  } else {
    const app = createApp(App)
    app.provide(process.env.VUE_APP_KEYCLOAK_PROVIDE_VARIABLE, keycloak)
    app.use(store)

    await store.dispatch('keycloakStore/fillRoles', keycloak.realmAccess.roles)

    app.use(router)
    app.mount('#app')
  }
}).catch((e) => {
  console.log('Serwer lezy: ' + e)
})

I have the above code in my main.js file.我的 main.js 文件中有上述代码。 This runs the keycloak subpage with login/register.这将运行带有登录/注册的 keycloak 子页面。 If someone succesfully logs in, my Vue app starts.如果有人成功登录,我的 Vue 应用程序就会启动。 Now, instead of the above, I'd like to make it so the Vue app starts regardless of the person being logged in and the keycloak screen is ONLY launched if someone clicks a specified button in the UI.现在,而不是上面的,我想让 Vue 应用程序启动而不管用户是否登录,并且只有当有人点击 UI 中的指定按钮时才会启动 keycloak 屏幕。 I've searched examples on the net, but i can only find ones that run keycloak on application start.我在网上搜索了示例,但我只能找到在应用程序启动时运行 keycloak 的示例。 Can someone help?有人可以帮忙吗?

Turn onLoad value from 'login-required' to 'check-sso'.将 onLoad 值从“需要登录”设置为“检查 sso”。 This option will only verify if the user is logged in without redirecting the user to login, and setup authentication properties acordingly.此选项将仅验证用户是否已登录,而不将用户重定向到登录,并相应地设置身份验证属性。

Then you can call keycloak.login(), for example in your code.然后您可以调用 keycloak.login(),例如在您的代码中。 Together with route guards if necessary.必要时与路线守卫一起。

Also, put the creation of the app out of the else block, because it will not load if the user is not logged in when you have 'check-sso'.此外,将应用程序的创建放在 else 块之外,因为如果在您拥有“check-sso”时用户未登录,它将不会加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM