"use client"; import React from "react"; /** * Auth Context (RHL-020) * * Purpose: * - Provide a tiny, app-wide session state for the UI: * - status: "unknown" | "loading" | "authenticated" | "unauthenticated" | "error" * - user: { userId, role, branchId } | null * - error: string | null * - retry: () => void | null (optional callback to re-run the session check) * * Why this file exists: * - Keep auth state accessible without prop-drilling. * - Keep the context/hook independent from Next.js routing. */ /** * @typedef {"unknown"|"loading"|"authenticated"|"unauthenticated"|"error"} AuthStatus */ /** * @typedef {Object} AuthUser * @property {string} userId * @property {string} role * @property {string|null} branchId */ /** * @typedef {Object} AuthState * @property {AuthStatus} status * @property {AuthUser|null} user * @property {string|null} error * @property {(() => void)|null} retry */ /** @type {AuthState} */ export const DEFAULT_AUTH_STATE = Object.freeze({ status: "unknown", user: null, error: null, retry: null, }); const AuthContext = React.createContext(DEFAULT_AUTH_STATE); /** * Consume the auth context. * * @returns {AuthState} */ export function useAuth() { return React.useContext(AuthContext); } /** * Provider wrapper. * * @param {{ value: Partial, children: React.ReactNode }} props */ export function AuthProvider({ value, children }) { const merged = React.useMemo(() => { return { ...DEFAULT_AUTH_STATE, ...(value || {}) }; }, [value]); return {children}; }