Files
b2b/bo/src/router/index.ts
2026-03-23 10:12:55 +01:00

81 lines
3.3 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'
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('../components/admin/ProductsView.vue'), name: 'products' },
{ path: 'products-datail/', component: () => import('../components/admin/ProductDetailView.vue'), name: 'product-detail' },
{ path: 'product-card-full/', component: () => import('../components/customer/PageProductCardFull.vue'), name: 'product-card-full' },
{ path: 'addresses', component: () => import('../components/customer/PageAddresses.vue'), name: 'addresses' },
{ path: 'cart', component: () => import('../components/customer/PageCart.vue'), name: 'cart' },
{ path: 'cart1', component: () => import('../components/customer/Cart1.vue'), name: 'cart1' },
{ path: 'products-list', component: () => import('../components/customer/PageProductsList.vue'), name: 'products-list' },
],
},
{
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 } },
],
},
],
},
],
})
router.beforeEach((to, from, next) => {
const locale = to.params.locale as string
const localeLang = langs.find((x) => x.iso_code == locale)
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
if (!to.meta?.guest && !isAuthenticated()) {
return next({ name: 'login', params: { locale } })
}
return next()
} else if (locale) {
return next(`/${currentLang.value?.iso_code}${to.path.replace(`/${locale}`, '') || '/'}`)
}
}
if (!locale && to.path !== '/') {
return next(`/${currentLang.value?.iso_code}${to.path}`)
}
next()
})
export default router