import { createRouter, createWebHistory } from 'vue-router' import type { RouteRecordRaw } from 'vue-router' import { useAuthStore } from '../stores/auth' const routes: RouteRecordRaw[] = [ { path: '/login', name: 'Login', component: () => import('../views/LoginView.vue'), meta: { public: true }, }, { path: '/', component: () => import('../layouts/MainLayout.vue'), meta: { requiresAuth: true }, children: [ { path: '', name: 'Home', redirect: '/connections', }, { path: 'transfers', name: 'Transfers', component: () => import('../views/TransfersView.vue'), }, { path: 'connections', name: 'Connections', component: () => import('../views/ConnectionsView.vue'), }, { path: 'terminal', name: 'TerminalWorkspace', component: () => import('../views/TerminalWorkspaceView.vue'), }, { path: 'terminal/:id', name: 'Terminal', component: () => import('../views/TerminalView.vue'), }, { path: 'sftp/:id', name: 'Sftp', component: () => import('../views/SftpView.vue'), }, ], }, ] const router = createRouter({ history: createWebHistory(), routes, }) router.beforeEach(async (to, _from, next) => { const authStore = useAuthStore() if (to.meta.public) { if (authStore.isAuthenticated && to.path === '/login') { next('/connections') } else { next() } return } if (to.meta.requiresAuth && !authStore.isAuthenticated) { next('/login') return } next() }) export default router