TaiBIF / camera-trap-webapp

[deprecated] Vue.js front end for camera trap data management app
2 stars 2 forks source link

router.js 中的 beforeEach 身份驗證問題 #153

Open trashmai opened 5 years ago

trashmai commented 5 years ago

src/pages/index/router.js 裡的 store 在頁面載入前似乎不會有值。因此若直接連需要驗證身份的連結如 http://localhost:8888/#/camera/d8064aa7-9643-44fb-bed9-1f23a690f968/edit 時,即使是處在登入狀態,也會得到 unauthorized 的錯誤訊息然後被導回計畫總覽頁。

預期行為應該是若處在登入狀態,可以直接開啟需要驗證身份的頁面。

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.managerOnly)) {
    const currentProjectId = store.state.project.currentProjectId;
    const projectRoles = store.state.auth.profile.project_roles || [];
    const currentRole = projectRoles.find(
      role => role.projectId === currentProjectId,
    );
    if (!currentRole || !isAllowManageProject(currentRole.role)) {
      console.log('* un-authorize redirect to overview page', currentRole);
      router.replace({
        name: 'overview',
      });
    } else {
      next();
    }
  } else {
    next();
  }
});