fix: RequireAuth hydration mismatch using Show component

- Use module-level signal for clientReady to persist across re-renders
- Use Show component for reactive rendering instead of manual null returns
- createEffect handles redirect when client is ready but no token
- onMount sets clientReady after hydration completes
- Dashboard now shows correct TUTOR role sidebar with Leads/My Responses
- Playwright test verifies TUTOR dashboard renders with correct sidebar items
This commit is contained in:
Tracewebstudio Dev 2026-04-22 00:54:08 +02:00
parent aa79b30465
commit f1e46e35e7
2 changed files with 44 additions and 29 deletions

View file

@ -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 (
<Show when={authClientReady() && !!getToken()} fallback={null}>
{props.children}
</Show>
);
}
export { getToken, clearAuthStorage };

View file

@ -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");
});