From 9fbb028124cf73a0b932fe3188b927a564ead36c Mon Sep 17 00:00:00 2001 From: Pan Date: Wed, 2 May 2018 17:37:47 +0800 Subject: [PATCH] perf[permission]: add the verification of roles --- src/permission.js | 6 +++--- src/store/modules/user.js | 8 +++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/permission.js b/src/permission.js index 87a591e5..e4f491d1 100644 --- a/src/permission.js +++ b/src/permission.js @@ -31,10 +31,10 @@ router.beforeEach((to, from, next) => { router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表 next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record }) - }).catch(() => { + }).catch((err) => { store.dispatch('FedLogOut').then(() => { - Message.error('Verification failed, please login again') - next({ path: '/login' }) + Message.error(err || 'Verification failed, please login again') + next({ path: '/' }) }) }) } else { diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 18c93570..13e76399 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -67,7 +67,13 @@ const user = { reject('error') } const data = response.data - commit('SET_ROLES', data.roles) + + if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组 + commit('SET_ROLES', data.roles) + } else { + reject('getInfo: roles must be a non-null array !') + } + commit('SET_NAME', data.name) commit('SET_AVATAR', data.avatar) commit('SET_INTRODUCTION', data.introduction)