import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { createPinia } from 'pinia'
import { createApp, onMounted, computed } from 'vue'
import App from '@/App.vue'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
import RegisterView from '../views/RegisterView.vue'
import DetailView from '../views/DetailView.vue'
import PricingView from '../views/PricingView.vue'
import SuccessView from '../views/SuccessView.vue'
import CategoriesView from '../views/CategoriesView.vue'
import CategoryItemsView from '../views/CategoryItemsView.vue'
const pinia = createPinia()
const app = createApp(App)
app.use(pinia)
const userStore = useUserStore();
userStore.fetchUser();
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/',
name: 'home',
component: HomeView
},
{
path: '/login',
name: 'login',
component: LoginView
},
{
path: '/register',
name: 'register',
component: RegisterView
},
{
path: '/categories',
name: 'categories',
component: CategoriesView
},
{
path: '/categories/:id',
name: 'category-items',
component: CategoryItemsView
},
{
path: '/product/:id',
name: 'product',
component: DetailView
},
{
path: '/pricing',
name: 'pricing',
component: PricingView
},
{
path: '/success',
name: 'success',
component: SuccessView
},
]
});
router.beforeEach((to, from, next) => {
const isLoggedIn = userStore.isLoggedIn;
console.log(isLoggedIn);
if (to.name === 'login' && isLoggedIn){
console.log(isLoggedIn);
next({ name: 'home' });
}
else{
next();
}
});
export default router