Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 1 | import Vue from 'vue'; |
| 2 | import VueRouter from 'vue-router'; |
Yoshie Muranaka | 8263d85 | 2020-10-16 07:58:06 -0700 | [diff] [blame] | 3 | |
| 4 | //Do not change store or routes import. |
| 5 | //Exact match alias set to support |
| 6 | //dotenv customizations. |
Yoshie Muranaka | 8263d85 | 2020-10-16 07:58:06 -0700 | [diff] [blame] | 7 | import store from '../store'; |
Dixsie Wolmers | 0a5b9c6 | 2020-10-20 10:52:56 -0500 | [diff] [blame] | 8 | import routes from './routes'; |
Derick Montague | a2988f4 | 2020-01-17 13:46:30 -0600 | [diff] [blame] | 9 | |
| 10 | Vue.use(VueRouter); |
Derick Montague | a2988f4 | 2020-01-17 13:46:30 -0600 | [diff] [blame] | 11 | const router = new VueRouter({ |
Derick Montague | a2988f4 | 2020-01-17 13:46:30 -0600 | [diff] [blame] | 12 | base: process.env.BASE_URL, |
| 13 | routes, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 14 | linkExactActiveClass: 'nav-link--current', |
Derick Montague | a2988f4 | 2020-01-17 13:46:30 -0600 | [diff] [blame] | 15 | }); |
| 16 | |
Damian Celico | aeb1981 | 2022-11-24 02:00:53 +0100 | [diff] [blame] | 17 | function allowRouterToNavigate(to, next, currentUserRole) { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 18 | if (to.matched.some((record) => record.meta.requiresAuth)) { |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 19 | if (store.getters['authentication/isLoggedIn']) { |
Damian Celico | aeb1981 | 2022-11-24 02:00:53 +0100 | [diff] [blame] | 20 | if (to.meta.exclusiveToRoles) { |
| 21 | // The privilege for the specific router was verified using the |
| 22 | // exclusiveToRoles roles in the router. |
| 23 | if (to.meta.exclusiveToRoles.includes(currentUserRole)) { |
| 24 | next(); |
| 25 | } else { |
| 26 | next('*'); |
| 27 | } |
| 28 | return; |
| 29 | } |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 30 | next(); |
| 31 | return; |
| 32 | } |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 33 | next('/login'); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 34 | } else { |
| 35 | next(); |
| 36 | } |
Damian Celico | aeb1981 | 2022-11-24 02:00:53 +0100 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | router.beforeEach((to, from, next) => { |
| 40 | let currentUserRole = store.getters['global/userPrivilege']; |
| 41 | // condition will get satisfied if user refreshed after login |
| 42 | if (!currentUserRole && store.getters['authentication/isLoggedIn']) { |
| 43 | // invoke API call to get the role ID |
| 44 | let username = localStorage.getItem('storedUsername'); |
| 45 | store.dispatch('authentication/getUserInfo', username).then((response) => { |
| 46 | if (response?.RoleId) { |
| 47 | // set role ID |
| 48 | store.commit('global/setPrivilege', response.RoleId); |
| 49 | // allow the route to continue |
| 50 | allowRouterToNavigate(to, next, response.RoleId); |
| 51 | } |
| 52 | }); |
| 53 | } else { |
| 54 | allowRouterToNavigate(to, next, currentUserRole); |
| 55 | } |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 56 | }); |
| 57 | |
Derick Montague | a2988f4 | 2020-01-17 13:46:30 -0600 | [diff] [blame] | 58 | export default router; |