blob: 4e962fea406f25ff8b75e6c73b0b7d5c1db2bdb0 [file] [log] [blame]
Derick Montaguefded0d12019-12-11 06:16:40 -06001import Vue from 'vue';
2import VueRouter from 'vue-router';
Yoshie Muranaka8263d852020-10-16 07:58:06 -07003
4//Do not change store or routes import.
5//Exact match alias set to support
6//dotenv customizations.
Yoshie Muranaka8263d852020-10-16 07:58:06 -07007import store from '../store';
Dixsie Wolmers0a5b9c62020-10-20 10:52:56 -05008import routes from './routes';
Derick Montaguea2988f42020-01-17 13:46:30 -06009
10Vue.use(VueRouter);
Derick Montaguea2988f42020-01-17 13:46:30 -060011const router = new VueRouter({
Derick Montaguea2988f42020-01-17 13:46:30 -060012 base: process.env.BASE_URL,
13 routes,
Derick Montague602e98a2020-10-21 16:20:00 -050014 linkExactActiveClass: 'nav-link--current',
aravinths1b3255412024-05-22 15:52:20 +053015 scrollBehavior() {
16 return { x: 0, y: 0 };
17 },
Derick Montaguea2988f42020-01-17 13:46:30 -060018});
19
Damian Celicoaeb19812022-11-24 02:00:53 +010020function allowRouterToNavigate(to, next, currentUserRole) {
Derick Montague602e98a2020-10-21 16:20:00 -050021 if (to.matched.some((record) => record.meta.requiresAuth)) {
Derick Montaguefded0d12019-12-11 06:16:40 -060022 if (store.getters['authentication/isLoggedIn']) {
Damian Celicoaeb19812022-11-24 02:00:53 +010023 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 Montaguee080a1a2019-12-04 16:30:08 -060033 next();
34 return;
35 }
Derick Montaguefded0d12019-12-11 06:16:40 -060036 next('/login');
Derick Montaguee080a1a2019-12-04 16:30:08 -060037 } else {
38 next();
39 }
Damian Celicoaeb19812022-11-24 02:00:53 +010040}
41
42router.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 Fertserbceaffa2024-04-10 16:27:53 +000048 store.dispatch('authentication/getUserInfo', username).then(() => {
49 let currentUserRole = store.getters['global/userPrivilege'];
50 allowRouterToNavigate(to, next, currentUserRole);
Damian Celicoaeb19812022-11-24 02:00:53 +010051 });
52 } else {
53 allowRouterToNavigate(to, next, currentUserRole);
54 }
Derick Montaguee080a1a2019-12-04 16:30:08 -060055});
56
Derick Montaguea2988f42020-01-17 13:46:30 -060057export default router;