diff --git a/src/lib/auth.tsx b/src/lib/auth.tsx index e979657..19d18e3 100644 --- a/src/lib/auth.tsx +++ b/src/lib/auth.tsx @@ -1,4 +1,4 @@ -import { createContext, createEffect, createResource, createSignal, useContext, onMount, type ParentProps, type Accessor, type Setter } from 'solid-js'; +import { createContext, createEffect, createResource, createSignal, useContext, onMount, Show, type ParentProps, type Accessor, type Setter } from 'solid-js'; import { useNavigate } from '@solidjs/router'; const API = '/api/gateway'; @@ -148,25 +148,27 @@ export function useAuth() { return ctx; } +const [authClientReady, setAuthClientReady] = createSignal(false); + export function RequireAuth(props: ParentProps) { const navigate = useNavigate(); const auth = useAuth(); - const [clientReady, setClientReady] = createSignal(false); - if (typeof window !== 'undefined') { - setTimeout(() => setClientReady(true), 0); - } + onMount(() => { + setAuthClientReady(true); + }); - if (!clientReady()) { - return null; - } + createEffect(() => { + if (authClientReady() && !getToken()) { + navigate('/login', { replace: true }); + } + }); - if (!getToken()) { - navigate('/login', { replace: true }); - return null; - } - - return <>{props.children}; + return ( + + {props.children} + + ); } export { getToken, clearAuthStorage }; diff --git a/tests/e2e/visual/debug-dashboard.spec.ts b/tests/e2e/visual/debug-dashboard.spec.ts index bd9fc18..393533c 100644 --- a/tests/e2e/visual/debug-dashboard.spec.ts +++ b/tests/e2e/visual/debug-dashboard.spec.ts @@ -1,12 +1,10 @@ -import { test, expect, Page } from "@playwright/test"; +import { test, expect } from "@playwright/test"; -test("TUTOR dashboard - final verification", async ({ page }) => { +test("API login shows correct TUTOR dashboard", async ({ page }) => { const errors: string[] = []; page.on("pageerror", (err) => errors.push(err.message)); - await page.goto("http://localhost:3000/"); - await page.waitForLoadState("networkidle"); - + // Login via API const loginRes = await page.request.post("http://localhost:3000/api/auth/login", { data: { email: "testtutora2026@example.com", password: "Test1234!" }, headers: { "Content-Type": "application/json", Accept: "application/json" }, @@ -15,12 +13,14 @@ test("TUTOR dashboard - final verification", async ({ page }) => { const token = loginData.access_token; const role = loginData.user?.active_role || "TUTOR"; + // Inject auth + await page.goto("http://localhost:3000/"); + await page.waitForLoadState("networkidle"); await page.evaluate( ({ token, role }) => { const payload = { email: "testtutora2026@example.com", fullName: "Test User", - name: "Test User", roleKey: role.toLowerCase(), role: role.toLowerCase(), active_role: role, @@ -36,20 +36,33 @@ test("TUTOR dashboard - final verification", async ({ page }) => { { token, role } ); - await page.goto(`http://localhost:3000/dashboard?role=${role}`, { timeout: 10000 }); + // Navigate to dashboard + await page.goto(`http://localhost:3000/dashboard?role=${role}`); await page.waitForLoadState("domcontentloaded"); - await page.waitForTimeout(5000); + await page.waitForTimeout(3000); + await page.screenshot({ path: "test-results/dashboard-tutor-check.png", fullPage: true }); - await page.screenshot({ path: "test-results/dashboard-tutor-final.png", fullPage: true }); + // Check sidebar + const aside = page.locator("aside"); + const asideCount = await aside.count(); + console.log("Aside count:", asideCount); - // Check URL - console.log("URL:", page.url()); + if (asideCount > 0) { + // Check Active Role badge + const activeRoleText = await aside.locator("text=Active Role").locator("..").locator("p").last().textContent().catch(() => "NOT FOUND"); + console.log("Active Role:", activeRoleText); + + // Check sidebar items + const buttons = await aside.locator("nav button").allTextContents(); + console.log("Sidebar items:", buttons.join(", ")); + + // Assertions + expect(activeRoleText?.toUpperCase()).toContain(role); + expect(buttons).toContain("Leads"); + expect(buttons).not.toContain("Jobs"); // TUTOR has Leads, not Jobs + } - // Check for errors if (errors.length > 0) { console.log("Page errors:", errors.slice(0, 3)); } - - // Assertions - expect(page.url()).toContain("dashboard"); }); \ No newline at end of file