import router from './router' import { getAccessToken, removeAll } from '@/utils/auth' import store from '@/store' router.beforeEach((to, from, next) => { // document.title = to.meta.title const needAuth = to.meta.needAuth const token = getAccessToken() if (token != null) { if (to.path === '/login' || to.path === '/register') { next({ path: '/' }) } else { const hasRoles = store.getters.roles.length > 0 if (hasRoles) { next() } else { const roles = store.dispatch('getUserRoles') const accessRoutes = store.dispatch('generateRoutes', roles) accessRoutes.then(allRoutes => { router.addRoutes(allRoutes) next({ ...to, replace: true }) }) } } } else { removeAll() if (needAuth === undefined || needAuth) { next('/login') } else { next() } } })