internal/dashboard/frontend/src/main.ts

import { createPinia, setActivePinia } from "pinia";
import { createApp } from "vue";
import { createRouter, createWebHistory } from "vue-router";
import App from "./App.vue";
import { routes } from "./routes";
import { useAuthStore } from "./store";
import "./style.css";

const router = createRouter({
  history: createWebHistory(),
  routes,
});

const pinia = createPinia();
setActivePinia(pinia);

router.beforeEach(async (to) => {
  const authStore = useAuthStore();

  // Only fetch if we haven't checked yet
  if (!authStore.statusChecked) {
    await authStore.fetchAuthStatus();
  }

  if (
    authStore.authRequired &&
    !authStore.authenticated &&
    to.path !== "/login"
  ) {
    return "/login";
  }

  if (authStore.authenticated && to.path === "/login") {
    return "/";
  }
});

createApp(App).use(router).use(pinia).mount("#app");