Ed Tanous | 883a0d5 | 2024-03-23 14:56:34 -0700 | [diff] [blame] | 1 | import { createRouter, createWebHistory } from 'vue-router'; |
Yoshie Muranaka | 8263d85 | 2020-10-16 07:58:06 -0700 | [diff] [blame] | 2 | |
| 3 | //Do not change store or routes import. |
| 4 | //Exact match alias set to support |
| 5 | //dotenv customizations. |
Yoshie Muranaka | 8263d85 | 2020-10-16 07:58:06 -0700 | [diff] [blame] | 6 | import store from '../store'; |
Dixsie Wolmers | 0a5b9c6 | 2020-10-20 10:52:56 -0500 | [diff] [blame] | 7 | import routes from './routes'; |
Derick Montague | a2988f4 | 2020-01-17 13:46:30 -0600 | [diff] [blame] | 8 | |
Ed Tanous | 883a0d5 | 2024-03-23 14:56:34 -0700 | [diff] [blame] | 9 | const router = createRouter({ |
| 10 | history: createWebHistory(process.env.BASE_URL), |
Derick Montague | a2988f4 | 2020-01-17 13:46:30 -0600 | [diff] [blame] | 11 | routes, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 12 | linkExactActiveClass: 'nav-link--current', |
aravinths1 | b325541 | 2024-05-22 15:52:20 +0530 | [diff] [blame] | 13 | scrollBehavior() { |
| 14 | return { x: 0, y: 0 }; |
| 15 | }, |
Derick Montague | a2988f4 | 2020-01-17 13:46:30 -0600 | [diff] [blame] | 16 | }); |
| 17 | |
Damian Celico | aeb1981 | 2022-11-24 02:00:53 +0100 | [diff] [blame] | 18 | function allowRouterToNavigate(to, next, currentUserRole) { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 19 | if (to.matched.some((record) => record.meta.requiresAuth)) { |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 20 | if (store.getters['authentication/isLoggedIn']) { |
Damian Celico | aeb1981 | 2022-11-24 02:00:53 +0100 | [diff] [blame] | 21 | if (to.meta.exclusiveToRoles) { |
| 22 | // The privilege for the specific router was verified using the |
| 23 | // exclusiveToRoles roles in the router. |
| 24 | if (to.meta.exclusiveToRoles.includes(currentUserRole)) { |
| 25 | next(); |
| 26 | } else { |
| 27 | next('*'); |
| 28 | } |
| 29 | return; |
| 30 | } |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 31 | next(); |
| 32 | return; |
| 33 | } |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 34 | next('/login'); |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 35 | } else { |
| 36 | next(); |
| 37 | } |
Damian Celico | aeb1981 | 2022-11-24 02:00:53 +0100 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | router.beforeEach((to, from, next) => { |
| 41 | let currentUserRole = store.getters['global/userPrivilege']; |
| 42 | // condition will get satisfied if user refreshed after login |
| 43 | if (!currentUserRole && store.getters['authentication/isLoggedIn']) { |
| 44 | // invoke API call to get the role ID |
Paul Fertser | ce7db82 | 2024-07-05 11:04:04 +0000 | [diff] [blame^] | 45 | store |
| 46 | .dispatch('authentication/getSessionPrivilege') |
| 47 | .then(() => { |
| 48 | let currentUserRole = store.getters['global/userPrivilege']; |
| 49 | allowRouterToNavigate(to, next, currentUserRole); |
| 50 | }) |
| 51 | // our store got out of sync, start afresh |
| 52 | .catch(() => { |
| 53 | console.log('Failed to obtain current Roles, logging out.'); |
| 54 | store.dispatch('authentication/logout'); |
| 55 | }); |
Damian Celico | aeb1981 | 2022-11-24 02:00:53 +0100 | [diff] [blame] | 56 | } else { |
| 57 | allowRouterToNavigate(to, next, currentUserRole); |
| 58 | } |
Derick Montague | e080a1a | 2019-12-04 16:30:08 -0600 | [diff] [blame] | 59 | }); |
| 60 | |
Derick Montague | a2988f4 | 2020-01-17 13:46:30 -0600 | [diff] [blame] | 61 | export default router; |