import { createRouter, createWebHistory } from 'vue-router' import { currentLang, langs } from './langs' import { getSettings } from './settings' import { useAuthStore } from '@/stores/auth' import Default from '@/layouts/default.vue' import { getMenu } from './menu' function isAuthenticated(): boolean { if (typeof document === 'undefined') return false return document.cookie.split('; ').some((c) => c === 'is_authenticated=1') } await getSettings() // await getMenu() const router = createRouter({ history: createWebHistory(import.meta.env.VITE_BASE_URL), routes: [ { path: '/', redirect: () => `/${currentLang.value?.iso_code}`, }, { path: '/:locale', children: [ { path: 'category/:category_id-:link_rewrite', component: () => import('../views/CategoryView.vue'), name: 'category' }, { 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: '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 } }, // { // path: '', // component: Empty, // children: [ // ], // }, { path: '/:pathMatch(.*)*', component: () => import('@/views/NotFoundView.vue'), name: 'not-found', }, ], }, { path: '/:pathMatch(.*)*', component: () => import('@/views/NotFoundView.vue'), name: 'not-found', }, ], }) // 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() // 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() // }) router.beforeEach((to, from) => { const locale = to.params.locale as string const localeLang = langs.find((x) => x.iso_code === locale) if (locale && langs.length > 0) { const authStore = useAuthStore() const validLocale = langs.find((l) => l.lang_code === locale) if (validLocale) { currentLang.value = localeLang if (!to.meta?.guest && !authStore.isAuthenticated) { return { name: 'login', params: { locale } } } return true } else if (locale) { return `/${currentLang.value?.iso_code}${to.path.replace(`/${locale}`, '') || '/'}` } } if (!locale && to.path !== '/') { return `/${currentLang.value?.iso_code}${to.path}` } return true }) export default router