import type { Component, FunctionPlugin } from 'vue' import { useAuth } from '@shared/composables/use-auth' import { createRouter, createWebHistory } from 'vue-router' import { routes } from 'vue-router/auto-routes' declare module 'vue-router' { interface RouteMeta { layout?: Component auth?: false | 'guest' } } export const router = createRouter({ history: createWebHistory(), routes, }) router.onError((e) => { console.log('wtf', e) }) router.isReady().then(() => { router.beforeEach((to) => { const { authorized } = useAuth() if (authorized.value && to.meta.auth === 'guest') { return { name: '/', replace: true } } if (!authorized.value && to.meta.auth === undefined) { return { name: '/auth/login', replace: true } } }) }) export default { install(app) { app.use(router) }, } as FunctionPlugin