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', |
aravinths1 | b325541 | 2024-05-22 15:52:20 +0530 | [diff] [blame] | 15 | scrollBehavior() { |
| 16 | return { x: 0, y: 0 }; |
| 17 | }, |
Derick Montague | a2988f4 | 2020-01-17 13:46:30 -0600 | [diff] [blame] | 18 | }); |
| 19 | |
Damian Celico | aeb1981 | 2022-11-24 02:00:53 +0100 | [diff] [blame] | 20 | function allowRouterToNavigate(to, next, currentUserRole) { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 21 | if (to.matched.some((record) => record.meta.requiresAuth)) { |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 22 | if (store.getters['authentication/isLoggedIn']) { |
Damian Celico | aeb1981 | 2022-11-24 02:00:53 +0100 | [diff] [blame] | 23 | if (to.meta.exclusiveToRoles) { |
| 24 | // The privilege for the specific router was verified using the |
| 25 | // exclusiveToRoles roles in the router. |
| 26 | if (to.meta.exclusiveToRoles.includes(currentUserRole)) { |
| 27 | next(); |
| 28 | } else { |
| 29 | next('*'); |
| 30 | } |
| 31 | return; |
| 32 | } |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 33 | next(); |
| 34 | return; |
| 35 | } |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 36 | next('/login'); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 37 | } else { |
| 38 | next(); |
| 39 | } |
Damian Celico | aeb1981 | 2022-11-24 02:00:53 +0100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | router.beforeEach((to, from, next) => { |
| 43 | let currentUserRole = store.getters['global/userPrivilege']; |
| 44 | // condition will get satisfied if user refreshed after login |
| 45 | if (!currentUserRole && store.getters['authentication/isLoggedIn']) { |
| 46 | // invoke API call to get the role ID |
| 47 | let username = localStorage.getItem('storedUsername'); |
Paul Fertser | bceaffa | 2024-04-10 16:27:53 +0000 | [diff] [blame] | 48 | store.dispatch('authentication/getUserInfo', username).then(() => { |
| 49 | let currentUserRole = store.getters['global/userPrivilege']; |
| 50 | allowRouterToNavigate(to, next, currentUserRole); |
Damian Celico | aeb1981 | 2022-11-24 02:00:53 +0100 | [diff] [blame] | 51 | }); |
| 52 | } else { |
| 53 | allowRouterToNavigate(to, next, currentUserRole); |
| 54 | } |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 55 | }); |
| 56 | |
Derick Montague | a2988f4 | 2020-01-17 13:46:30 -0600 | [diff] [blame] | 57 | export default router; |