Authentication Flow & Sessions
Relevant source files
The following files were used as context for generating this wiki page:
The Authentication Flow & Sessions system manages user access and identity within the Lou Gehrig Fan Club (LGFC) repository. It distinguishes between general public access, authenticated FanClub members, and privileged administrators.
The system relies on a hybrid approach using client-side localStorage for basic member session persistence and a server-side token-based system for administrative functions. Authenticated routes, particularly those under /fanclub/**, are protected by client-side hooks that enforce redirection for unauthenticated traffic.
Sources: context.md:21-25, src/hooks/useMemberSession.ts:1-12
Member Authentication
Member authentication is primarily handled client-side to maintain "Lou Gehrig Fan Club" area invariants. A user is considered authenticated if a specific key, lgfc_member_email, exists in the browser's localStorage.
Session Management Component
The useMemberSession hook serves as the primary interface for session state. It provides loading status, authentication flags, and user metadata (email and role) to protected pages like /fanclub/page.tsx.
| Property | Type | Description |
|---|---|---|
isAuthenticated | boolean | True if lgfc_member_email exists in storage. |
isLoading | boolean | True while the hook is initially checking storage. |
email | string | The value retrieved from lgfc_member_email. |
role | string | The member's role (e.g., 'admin') fetched from /api/member/role. |
Sources: context.md:22, src/hooks/useMemberSession.ts:14-25, src/app/fanclub/page.tsx:24
Login Flow
The login process transitions a user from an unauthenticated state to an authenticated state by verifying the user and setting local storage keys.
Administrative Authentication
Admin access is strictly separated from standard member access and uses a "fail-closed" security model. It requires a specific ADMIN_TOKEN configured in the environment and passed via custom request headers.
Admin Token Enforcement
Server-side functions for administrative tasks (like CMS management) utilize the requireAdmin helper. This helper validates the existence of the ADMIN_TOKEN in the environment and compares it against the x-admin-token header provided by the client.
Key Admin Components
| Component | Path | Auth Requirement |
|---|---|---|
| Admin Token Storage | localStorage | Uses key lgfc_admin_token for persistence in Admin UI. |
| CMS Editor | /admin/cms | Requires x-admin-token for list, save, and publish. |
| Role Verification | /api/member/role | Determines if a standard member has admin privileges. |
Sources: src/app/admin/cms/page.tsx:28, context.md:24, functions/_lib/auth.ts
Route Protection
The project implements specific navigation invariants to protect sensitive areas.
- FanClub Routes: Routes starting with
/fanclub/**require an active member session. IfuseMemberSessiondetects an unauthenticated state, it redirects the user to the public homepage (/). - Admin Routes: Routes under
/admin/**require the administrative token. The UI components for these pages (e.g.,AdminCMS) retrieve this token fromlgfc_admin_tokeninlocalStorageand include it in API requests.
Sources: context.md:23-24, src/hooks/useMemberSession.ts:10-15, src/app/admin/cms/page.tsx:50-55
Summary
The authentication system is split into a client-centric model for club members and a header-based token model for administrators. Member sessions are maintained through localStorage with client-side redirects protecting the /fanclub area. Administrative functions are secured on the server side via the requireAdmin utility, ensuring that content management operations in the content_blocks table remain restricted to authorized personnel only.