v0
This commit is contained in:
37
frontend/src/router/index.ts
Normal file
37
frontend/src/router/index.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import Login from '../views/Login.vue'
|
||||
import Setup from '../views/Setup.vue'
|
||||
import Dashboard from '../views/Dashboard.vue'
|
||||
|
||||
const routes = [
|
||||
{ path: '/login', component: Login },
|
||||
{ path: '/setup', component: Setup },
|
||||
{ path: '/', component: Dashboard, meta: { requiresAuth: true } }
|
||||
]
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
routes
|
||||
})
|
||||
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
|
||||
const loggedIn = await checkAuth() // запрос к /api/admin/users или специальному endpoint
|
||||
|
||||
if (requiresAuth && !loggedIn) {
|
||||
next('/login')
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
})
|
||||
|
||||
async function checkAuth(): Promise<boolean> {
|
||||
try {
|
||||
const res = await fetch('/api/admin/users')
|
||||
return res.ok
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export default router
|
||||
Reference in New Issue
Block a user