Overview
TheUserContext provides centralized user authentication state management for the CicloVital application. It maintains the current user’s data and automatically persists it to localStorage for session continuity.
Context Shape
Properties
The current authenticated user object, or
null if no user is logged in.Function to update the current user. Pass
null to log out.Provider Component
UserProvider
Wraps your application to provide user context to all child components.Features
- Automatic Persistence: User data is automatically saved to localStorage
- Session Recovery: Loads user data from localStorage on initialization
- Safe Storage: Includes error handling for localStorage operations
- Logout Support: Removes data from localStorage when user is set to
null
Usage
Consuming the Context
Setting User Data (Login)
Logging Out
Implementation Details
Source Files
src/contexts/UserContext.js- Context definitionsrc/contexts/UserProvider.jsx- Provider implementation
localStorage Key
The context stores user data under the key"user" in localStorage.
Initial State
On mount, the provider attempts to load user data from localStorage. If no data exists or parsing fails, it defaults tonull.
Best Practices
Related
- ChatContext - Manages chat state
- ThemeContext - Manages theme preferences
