Files
chad/new-client/src/app/plugins/router.ts
2026-05-14 01:05:01 +06:00

40 lines
869 B
TypeScript

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