blob: 5b6d9099c2afc968377572b228b7fb5080f36587 [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',
Derick Montaguea2988f42020-01-17 13:46:30 -060015});
16
Damian Celicoaeb19812022-11-24 02:00:53 +010017function allowRouterToNavigate(to, next, currentUserRole) {
Derick Montague602e98a2020-10-21 16:20:00 -050018 if (to.matched.some((record) => record.meta.requiresAuth)) {
Derick Montaguefded0d12019-12-11 06:16:40 -060019 if (store.getters['authentication/isLoggedIn']) {
Damian Celicoaeb19812022-11-24 02:00:53 +010020 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 Montaguee080a1a2019-12-04 16:30:08 -060030 next();
31 return;
32 }
Derick Montaguefded0d12019-12-11 06:16:40 -060033 next('/login');
Derick Montaguee080a1a2019-12-04 16:30:08 -060034 } else {
35 next();
36 }
Damian Celicoaeb19812022-11-24 02:00:53 +010037}
38
39router.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');
Paul Fertserbceaffa2024-04-10 16:27:53 +000045 store.dispatch('authentication/getUserInfo', username).then(() => {
46 let currentUserRole = store.getters['global/userPrivilege'];
47 allowRouterToNavigate(to, next, currentUserRole);
Damian Celicoaeb19812022-11-24 02:00:53 +010048 });
49 } else {
50 allowRouterToNavigate(to, next, currentUserRole);
51 }
Derick Montaguee080a1a2019-12-04 16:30:08 -060052});
53
Derick Montaguea2988f42020-01-17 13:46:30 -060054export default router;