Documentation Index
Fetch the complete documentation index at: https://mintlify.com/toryshchyn/cat-web/llms.txt
Use this file to discover all available pages before exploring further.
Overview
TheAuthGuard component is a higher-order component that wraps protected routes to ensure only authenticated users can access them. It integrates with Auth0 to check authentication status and handles loading, unauthenticated, and error states.
Source: src/components/AuthGuard.tsx
Key Features
- Checks user authentication status before rendering protected content
- Displays loading spinner during authentication check
- Redirects unauthenticated users to login page
- Handles authentication errors gracefully
- Seamlessly renders protected component when authenticated
Props
The component accepts a single prop defined by theAuthGuardProps interface:
The protected component to render when the user is authenticated.
TypeScript Interface
Authentication States
AuthGuard handles four distinct authentication states:1. Loading State
While Auth0 is checking authentication status, a centered loading spinner is displayed:2. Unauthenticated State
If the user is not authenticated, they are redirected to the Login component:3. Error State
If an authentication error occurs, an error message is displayed:4. Authenticated State
When successfully authenticated, the protected component is rendered:Usage Examples
Basic Usage with React Router
Protecting Multiple Routes
Custom Protected Page
Integration with Auth0
AuthGuard uses theuseAuth0 hook to access authentication state:
Required Setup
Ensure your app is wrapped with the Auth0 provider:Material-UI Components
AuthGuard uses the following Material-UI components:Box: Container for layout and stylingCircularProgress: Loading spinner during authentication check
Complete Source Code
Best Practices
- Wrap at Route Level: Apply AuthGuard at the route level rather than individual components
- Nested Protection: Protect parent routes to automatically protect all child routes
- Error Handling: Consider customizing the error UI for better user experience
- Loading State: The default loading spinner works well, but can be customized if needed
- Logging: Keep authentication logs for debugging purposes (already included)
Related Components
- Login Component - Handles authentication initiation
- MasterLayout Component - Main layout for authenticated users
