Files
ulitka-app/src/router/index.ts
T
2026-09-18 14:36:32 +03:00

74 lines
2.0 KiB
TypeScript

import { createRouter, createWebHistory } from 'vue-router'
import ProfileView from '@/views/users/ProfileView.vue'
import { issuesRoutes } from '@/router/issues.ts'
import { reportsRoutes } from '@/router/reports.ts'
import { summaryRoutes } from '@/router/summary.ts'
import HomeView from '@/views/HomeView.vue'
import VerifyEmailView from '@/views/users/VerifyEmailView.vue'
import SignUpView from '@/views/users/SignUpView.vue'
import SignInView from '@/views/users/SignInView.vue'
import { authClient } from '@/lib/auth-client.ts'
import { gamesRoutes } from '@/router/games.ts'
declare module 'vue-router' {
interface RouteMeta {
layout?: 'Blank' | 'Default'
}
}
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/',
component: HomeView,
meta: { layout: 'Default' },
},
{
name: 'sign-in',
path: '/sign-in',
component: SignInView,
},
{
path: '/sign-up',
component: SignUpView,
},
{
path: '/verify-email',
component: VerifyEmailView,
},
{
path: '/profile',
component: ProfileView,
meta: { requiresAuth: true },
},
issuesRoutes,
summaryRoutes,
reportsRoutes,
gamesRoutes,
],
})
router.beforeEach(async (to, from) => {
// 1. Fetch the current session from Better Auth
const { data: session } = await authClient.getSession()
// console.log('before each ', { session })
// const session = 'true'
const isAuthenticated = !!session
// 2. If the route requires auth and user is missing, redirect to login
if (to.meta.requiresAuth && !isAuthenticated) {
return { name: 'sign-in', query: { redirect: to.fullPath } }
}
// 3. If route requires a guest (like Login page) and user is logged in, redirect to dashboard
if (to.meta.requiresGuest && isAuthenticated) {
return { name: 'dashboard' }
}
// Implicitly returns undefined / true to allow navigation if conditions don't match
})
export default router