1*0c1dcc96SHuy Le Anhimport { createRouter, createWebHashHistory } from 'vue-router'; 28263d85cSYoshie Muranaka 38263d85cSYoshie Muranaka//Do not change store or routes import. 48263d85cSYoshie Muranaka//Exact match alias set to support 58263d85cSYoshie Muranaka//dotenv customizations. 68263d85cSYoshie Muranakaimport store from '../store'; 70a5b9c6dSDixsie Wolmersimport routes from './routes'; 8a2988f40SDerick Montague 9883a0d59SEd Tanousconst router = createRouter({ 10*0c1dcc96SHuy Le Anh history: createWebHashHistory(), 11a2988f40SDerick Montague routes, 12602e98aaSDerick Montague linkExactActiveClass: 'nav-link--current', 13b325541cSaravinths1 scrollBehavior() { 14b325541cSaravinths1 return { x: 0, y: 0 }; 15b325541cSaravinths1 }, 16a2988f40SDerick Montague}); 17a2988f40SDerick Montague 18aeb19816SDamian Celicofunction allowRouterToNavigate(to, next, currentUserRole) { 19602e98aaSDerick Montague if (to.matched.some((record) => record.meta.requiresAuth)) { 20fded0d11SDerick Montague if (store.getters['authentication/isLoggedIn']) { 21aeb19816SDamian Celico if (to.meta.exclusiveToRoles) { 22aeb19816SDamian Celico // The privilege for the specific router was verified using the 23aeb19816SDamian Celico // exclusiveToRoles roles in the router. 24aeb19816SDamian Celico if (to.meta.exclusiveToRoles.includes(currentUserRole)) { 25aeb19816SDamian Celico next(); 26aeb19816SDamian Celico } else { 27aeb19816SDamian Celico next('*'); 28aeb19816SDamian Celico } 29aeb19816SDamian Celico return; 30aeb19816SDamian Celico } 31e080a1a7SDerick Montague next(); 32e080a1a7SDerick Montague return; 33e080a1a7SDerick Montague } 34fded0d11SDerick Montague next('/login'); 35e080a1a7SDerick Montague } else { 36e080a1a7SDerick Montague next(); 37e080a1a7SDerick Montague } 38aeb19816SDamian Celico} 39aeb19816SDamian Celico 40aeb19816SDamian Celicorouter.beforeEach((to, from, next) => { 41aeb19816SDamian Celico let currentUserRole = store.getters['global/userPrivilege']; 42aeb19816SDamian Celico // condition will get satisfied if user refreshed after login 43aeb19816SDamian Celico if (!currentUserRole && store.getters['authentication/isLoggedIn']) { 44aeb19816SDamian Celico // invoke API call to get the role ID 45ce7db82cSPaul Fertser store 46ce7db82cSPaul Fertser .dispatch('authentication/getSessionPrivilege') 47ce7db82cSPaul Fertser .then(() => { 48bceaffacSPaul Fertser let currentUserRole = store.getters['global/userPrivilege']; 49bceaffacSPaul Fertser allowRouterToNavigate(to, next, currentUserRole); 50ce7db82cSPaul Fertser }) 51ce7db82cSPaul Fertser // our store got out of sync, start afresh 52ce7db82cSPaul Fertser .catch(() => { 53ce7db82cSPaul Fertser console.log('Failed to obtain current Roles, logging out.'); 54ce7db82cSPaul Fertser store.dispatch('authentication/logout'); 55aeb19816SDamian Celico }); 56aeb19816SDamian Celico } else { 57aeb19816SDamian Celico allowRouterToNavigate(to, next, currentUserRole); 58aeb19816SDamian Celico } 59e080a1a7SDerick Montague}); 60e080a1a7SDerick Montague 61a2988f40SDerick Montagueexport default router; 62