90 lines
3.1 KiB
TypeScript
90 lines
3.1 KiB
TypeScript
import { createRouter, createWebHistory } from 'vue-router'
|
|
import Empty from '@/layouts/empty.vue'
|
|
import { currentLang, langs } from './langs'
|
|
import { getSettings } from './settings'
|
|
import { useAuthStore } from '@/stores/auth'
|
|
import Default from '@/layouts/default.vue'
|
|
|
|
// Helper: read the non-HTTPOnly is_authenticated cookie set by the backend.
|
|
// The backend sets it to "1" on login and removes it on logout.
|
|
function isAuthenticated(): boolean {
|
|
if (typeof document === 'undefined') return false
|
|
return document.cookie.split('; ').some((c) => c === 'is_authenticated=1')
|
|
}
|
|
|
|
|
|
await getSettings()
|
|
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory(import.meta.env.VITE_BASE_URL),
|
|
routes: [
|
|
{
|
|
path: '/',
|
|
redirect: () => `/${currentLang.value?.iso_code}`,
|
|
},
|
|
{
|
|
path: '/:locale',
|
|
children: [
|
|
{
|
|
path: '',
|
|
component: Default,
|
|
children: [
|
|
{ path: '', component: () => import('../views/RepoChartView.vue'), name: 'home' },
|
|
{ path: 'products', component: () => import('../views/customer/ProductsView.vue'), name: 'products' },
|
|
{ path: 'products-datail/', component: () => import('../views/customer/ProductDetailView.vue'), name: 'product-detail' },
|
|
],
|
|
},
|
|
{
|
|
path: '',
|
|
component: Empty,
|
|
children: [
|
|
{ path: 'login', component: () => import('@/views/LoginView.vue'), name: 'login', meta: { guest: true } },
|
|
{ path: 'register', component: () => import('@/views/RegisterView.vue'), name: 'register', meta: { guest: true } },
|
|
{ path: 'password-recovery', component: () => import('@/views/PasswordRecoveryView.vue'), name: 'password-recovery', meta: { guest: true } },
|
|
{ path: 'reset-password', component: () => import('@/views/ResetPasswordForm.vue'), name: 'reset-password', meta: { guest: true } },
|
|
{ path: 'verify-email', component: () => import('@/views/VerifyEmailView.vue'), name: 'verify-email', meta: { guest: true } },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
})
|
|
|
|
// Navigation guard: language handling + auth protection
|
|
router.beforeEach((to, from, next) => {
|
|
const locale = to.params.locale as string
|
|
const localeLang = langs.find((x) => x.iso_code == locale)
|
|
|
|
// Check if the locale is valid
|
|
if (locale && langs.length > 0) {
|
|
const authStore = useAuthStore()
|
|
console.log(authStore.isAuthenticated,to, from)
|
|
// if()
|
|
const validLocale = langs.find((l) => l.lang_code === locale)
|
|
|
|
if (validLocale) {
|
|
currentLang.value = localeLang
|
|
|
|
// Auth guard: if the route does NOT have meta.guest = true, require authentication
|
|
if (!to.meta?.guest && !isAuthenticated()) {
|
|
return next({ name: 'login', params: { locale } })
|
|
}
|
|
|
|
return next()
|
|
} else if (locale) {
|
|
// Invalid locale - redirect to default language
|
|
return next(`/${currentLang.value?.iso_code}${to.path.replace(`/${locale}`, '') || '/'}`)
|
|
}
|
|
}
|
|
|
|
// No locale in URL - redirect to default language
|
|
if (!locale && to.path !== '/') {
|
|
return next(`/${currentLang.value?.iso_code}${to.path}`)
|
|
}
|
|
|
|
next()
|
|
})
|
|
|
|
export default router
|