| 37 | * @returns {Router|{x: number, y: number}} |
| 38 | */ |
| 39 | export const createRouter = () => { |
| 40 | if (!router) { |
| 41 | router = createVueRouter({ |
| 42 | history: createWebHistory(), |
| 43 | routes, |
| 44 | scrollBehavior() { |
| 45 | return { x: 0, y: 0 }; |
| 46 | }, |
| 47 | }); |
| 48 | |
| 49 | const originalPush = router.push; |
| 50 | router.push = function push(location) { |
| 51 | return originalPush |
| 52 | .call(this, location) |
| 53 | .catch((error) => { |
| 54 | console.error(error); |
| 55 | ProgressBar.done(); |
| 56 | }); |
| 57 | }; |
| 58 | |
| 59 | router.beforeEach(async (to, from, next) => { |
| 60 | if (to.name && to.name !== from.name) { |
| 61 | ProgressBar.start(); |
| 62 | } |
| 63 | const matchedRoute = to.matched.find( |
| 64 | (m) => m.meta.middleware, |
| 65 | ); |
| 66 | if (matchedRoute !== undefined) { |
| 67 | const context = { |
| 68 | from, |
| 69 | router, |
| 70 | to, |
| 71 | store, |
| 72 | next, |
| 73 | }; |
| 74 | |
| 75 | await authGuard(context); |
| 76 | await unauthGuard(context); |
| 77 | |
| 78 | next(); |
| 79 | } |
| 80 | }); |
| 81 | |
| 82 | router.afterEach(() => { |
| 83 | ProgressBar.done(); |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | return router; |
| 88 | }; |
| 89 | |
| 90 | export { |
| 91 | router, |