简体   繁体   English

casl - can() 在重新加载 (F5) 页面后返回 null

[英]casl - can() returns null after reloading (F5) page

I'm trying to setting up CASL permissions system into my VueJS 2 project.我正在尝试将 CASL 权限系统设置到我的 VueJS 2 项目中。 All works fine, until I intentionally refresh (Like F5).一切正常,直到我故意刷新(如 F5)。 All can() returns false even if my user have the abilities.即使我的用户有能力,所有 can() 都返回 false。

router / index.js:路由器/index.js:

router.beforeEach((to, _, next) => {
  const isLoggedIn = isUserLoggedIn()
  if (!canNavigate(to)) {
    // Redirect to login if not logged in
    if (!isLoggedIn) return next({ name: 'auth-login' })
    // If logged in => not authorized
    return next({ name: 'misc-not-authorized' })
  }

  // Redirect if logged in
  if (to.meta.redirectIfLoggedIn && isLoggedIn) {
    const userData = getUserData()
    next(getDashboardRoute(userData ? userData.role : null))
  }

  return next()
})

canNavigate(to) function:可以导航(到)function:

export const canNavigate = to => ability.can(to.meta.action || 'read', to.meta.resource)

user abilities (from localStorage):用户能力(来自 localStorage):

用户能力

route configuration:路线配置:

export default [
      {
        path: '/route-test/',
        name: 'route-test',
        component: () => import('@/views/TestRoute.vue'),
        meta: {
          resource: 'ADB',
          action: 'read',
        },
      },
    ]

So, canNavigate returns false, and I'm getting a Maximum call stack size exceeded error but, this is normal due to the "infinite" loop with in my beforeEach router function...因此,canNavigate 返回 false,并且我收到了最大调用堆栈大小超出错误但是,这是正常的,因为我的 beforeEach 路由器 function 中的“无限”循环...

Why do my canNavigate returns false... after refresh?为什么我的 canNavigate 刷新后返回 false...?

Thanks to everyone give time to help me:)感谢大家给我时间帮助我:)

I'm facing the same issue with a Vue 2 project - everytime i refresh the page the $ability instance gets reseted to its default value (before the.update() is called).我在 Vue 2 项目中遇到了同样的问题 - 每次我刷新页面时,$ability 实例都会重置为其默认值(在调用 the.update() 之前)。 Idk what's the root cause of the problem, but what i made solves the problem for now: I update the $ability instance each time the entry point component (in my case Home component) is mounted().我知道问题的根本原因是什么,但我现在解决了这个问题:每次安装入口点组件(在我的情况下是 Home 组件)时,我都会更新 $ability 实例。 Idk if it is a good solution, but worked for me (if anyone knows a better way to do it please let me know).如果这是一个很好的解决方案,但对我有用(如果有人知道更好的方法,请告诉我)。

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

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