Kaynağa Gözat

fix: robust logout with home redirect and router guards

unknown 6 gün önce
ebeveyn
işleme
045c40fbd2

+ 7 - 1
src/components/Header.vue

@@ -178,9 +178,15 @@ const navLinks = computed(() => [
 ]);
 
 async function handleLogout() {
+  const currentPath = router.currentRoute.value.path;
   await authStore.logout();
   toast.success(t("nav.loggedOut"));
-  router.push("/");
   mobileOpen.value = false;
+  
+  if (currentPath === "/" || currentPath.startsWith("/#")) {
+    window.location.reload(); // Refresh if already on home to clear all states
+  } else {
+    router.push("/");
+  }
 }
 </script>

+ 1 - 2
src/pages/Portfolio.vue

@@ -35,8 +35,7 @@
             class="w-full h-full object-cover transition-transform duration-700 group-hover:scale-110" />
           <div class="absolute inset-0 bg-gradient-to-t from-black/80 via-black/20 to-transparent opacity-0 group-hover:opacity-100 transition-all duration-500 flex flex-col justify-end p-6">
             <span class="text-[10px] font-bold uppercase tracking-widest text-primary mb-1">{{ item.material_name }}</span>
-            <div class="flex items-center justify-between">
-              <p class="text-white font-bold">{{ item.order_id ? t("common.orderId", { id: item.order_id }) : 'Radionica3D' }}</p>
+            <div class="flex items-center justify-end">
               <ExternalLink class="w-4 h-4 text-white/70" />
             </div>
           </div>

+ 18 - 0
src/router/index.ts

@@ -27,6 +27,24 @@ const router = createRouter({
   },
 });
 
+router.beforeEach(async (to) => {
+  const { useAuthStore } = await import("@/stores/auth");
+  const authStore = useAuthStore();
+  
+  // Ensure we check auth state
+  if (!authStore.user && localStorage.getItem("token")) {
+    await authStore.init();
+  }
+
+  if (to.path === "/admin" && authStore.user?.role !== "admin") {
+    return { path: "/auth" };
+  }
+  
+  if (to.path === "/orders" && !authStore.user) {
+    return { path: "/auth" };
+  }
+});
+
 router.afterEach((to) => {
   const baseTitle = "Radionica 3D";
   const pageTitle = to.meta.title ? `${to.meta.title} | ${baseTitle}` : baseTitle;

+ 9 - 6
src/stores/auth.ts

@@ -148,13 +148,16 @@ export const useAuthStore = defineStore("auth", () => {
   }
 
   async function logout() {
-    import("@/lib/api").then(async ({ logoutUser }) => {
+    const { logoutUser } = await import("@/lib/api");
+    try {
       await logoutUser();
-      localStorage.removeItem("token");
-      user.value = null;
-      unreadMessagesCount.value = 0;
-      stopPing();
-    });
+    } catch (e) {
+      console.error("Logout API call failed, continuing local cleanup", e);
+    }
+    localStorage.removeItem("token");
+    user.value = null;
+    unreadMessagesCount.value = 0;
+    stopPing();
   }
 
   async function refreshUnreadCount() {