// Authentication helper functions async function getCurrentUser() { try { const response = await fetch("/api/user/me", { credentials: "include", }); if (response.ok) { return await response.json(); } return null; } catch (error) { console.error("Error fetching current user:", error); return null; } } // Auto-login check async function checkAuth() { const user = await getCurrentUser(); // List of pages that do NOT require authentication const publicPages = ["/", "/index.html", "/login.html"]; // If user is NOT logged in AND we are NOT on a public page if (!user && !publicPages.includes(window.location.pathname)) { // Redirect immediately to the Sign In page window.location.href = "/login.html"; return null; } return user; } // Redirect to dashboard if already logged in (for login.html and index.html) async function redirectIfLoggedIn() { const user = await getCurrentUser(); if (user) { window.location.href = "/dashboard.html"; } } // Logout function function logout() { document.cookie = "browser_id=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT"; window.location.href = "/login.html"; } // Extract path parts for display function formatFullPath(fullPath) { return fullPath.replace(/<>/g, " - "); } // Parse full_path to extract parts (semester<>faculty<>field) function parseFullPath(fullPath) { const parts = fullPath.split("<>"); return { semester: parts[0] || "", faculty: parts[1] || "", field: parts[2] || "", }; } // Check subscription status async function checkSubscription(userId, fullPath) { try { const response = await fetch( `/api/subscription/check/${userId}/${encodeURIComponent(fullPath)}`, { credentials: "include", }, ); const data = await response.json(); return data.subscribed; } catch (error) { console.error("Error checking subscription:", error); return false; } } // WhatsApp redirect function redirectToWhatsApp(phone, fullPath) { const message = `Hello, I would like to activate access to: ${fullPath}`; const url = `https://wa.me/${phone}?text=${encodeURIComponent(message)}`; window.open(url, "_blank"); }