61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { createRouter, createWebHistory } from 'vue-router'
|
|
import { useAuthStore } from '@/stores/auth'
|
|
import UserLayout from '@/layouts/UserLayout.vue'
|
|
import AdminLayout from '@/layouts/AdminLayout.vue'
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory(),
|
|
routes: [
|
|
{
|
|
path: '/',
|
|
component: UserLayout,
|
|
meta: { public: true },
|
|
children: [
|
|
{ path: '', redirect: '/chat' },
|
|
{ path: 'chat', name: 'chat', component: () => import('@/views/ChatView.vue') },
|
|
{ path: 'image', name: 'image', component: () => import('@/views/ImageStudioView.vue') },
|
|
{ path: 'image/text', redirect: '/image' },
|
|
{ path: 'image/edit', redirect: '/image' },
|
|
{ path: 'history', name: 'history', component: () => import('@/views/HistoryView.vue') },
|
|
],
|
|
},
|
|
{
|
|
path: '/admin/login',
|
|
name: 'admin-login',
|
|
component: () => import('@/views/LoginView.vue'),
|
|
meta: { public: true },
|
|
},
|
|
{
|
|
path: '/admin',
|
|
component: AdminLayout,
|
|
children: [
|
|
{ path: '', name: 'admin-dashboard', component: () => import('@/views/AdminDashboardView.vue') },
|
|
{ path: 'providers', name: 'admin-providers', component: () => import('@/views/AdminProvidersView.vue') },
|
|
{ path: 'history', name: 'admin-history', component: () => import('@/views/AdminHistoryView.vue') },
|
|
{ path: 'logs', redirect: '/admin/history' },
|
|
{ path: 'settings', name: 'admin-settings', component: () => import('@/views/SettingsView.vue') },
|
|
],
|
|
},
|
|
{ path: '/login', redirect: '/admin/login' },
|
|
],
|
|
})
|
|
|
|
router.beforeEach(async (to) => {
|
|
if (to.meta.public) return true
|
|
if (!to.path.startsWith('/admin')) return true
|
|
|
|
const auth = useAuthStore()
|
|
if (auth.isLoggedIn()) return true
|
|
try {
|
|
const res = await fetch('/api/v1/auth/status')
|
|
const data = await res.json()
|
|
if (!data.enabled) {
|
|
auth.token = 'disabled'
|
|
return true
|
|
}
|
|
} catch { /* ignore */ }
|
|
return '/admin/login'
|
|
})
|
|
|
|
export default router
|