import VueRouter from "vue-router"; import routes from "./routes"; import { isAuthenticated, safeDestination } from '@/auth'; // configure router const router = new VueRouter({ routes, // short for routes: routes linkExactActiveClass: "active", scrollBehavior: (to) => { if (to.hash) { return {selector: to.hash} } else { return { x: 0, y: 0 } } } }); router.beforeEach((to, from, next) => { if (to.matched.some(record => record.meta.requiresAuth) && !isAuthenticated()) { next({ path: '/login', query: { redirect: to.fullPath }, replace: true }); } else if (to.name === 'login' && isAuthenticated()) { next({ path: safeDestination(to.query.redirect), replace: true }); } else next(); }); export default router;