import { useAuth } from '@/context/auth'; import { APP_ROLES, DEFAULT_ROLE, type AppRole } from '@/constants'; /** * True when the current session is authenticated. * * Reads AuthContext, which the root layout seeds from the request's access-token * cookie on the server. The value is therefore correct on the first render — no * post-mount cookie read, no hydration flash. */ export function useIsAuthenticated(): boolean { const [state] = useAuth(); return state.isAuthenticated; } // Precedence when a user holds several roles: the highest-privilege shell wins. const ROLE_PRECEDENCE: AppRole[] = [APP_ROLES.ADMIN, APP_ROLES.NURSE, APP_ROLES.CUSTOMER]; /** * The actor experience the current session should see, read from the session roles. * * Chrome-only signal: which nav/shell the shells build from the collapsed session roles. The DEFAULT_ROLE * (customer) fallback is a **last resort**, not the loading state — the shells are wrapped in `RoleGuard` * (refinement-phase-2), which holds a neutral splash until `/me` resolves and redirects a role mismatch, so * this is only ever read once the roles are hydrated. Never gate "which app" on this fallback; that decision * is `resolveRoleDestination` (via `RoleGuard`/`RoleRouter`), the single source of truth. */ export function useActorRole(): AppRole { const [state] = useAuth(); const roles = state.currentUser?.roles; if (!roles?.length) return DEFAULT_ROLE; return ROLE_PRECEDENCE.find((role) => roles.includes(role)) ?? DEFAULT_ROLE; }