Overview
Cat Web uses Auth0 for secure authentication, providing a seamless login experience with OAuth 2.0 and JWT token management. The application protects routes and API calls using Auth0’s React SDK.Auth0 Integration
The application wraps the entire app with theAuth0Provider in the main entry point:
src/main.tsx
Environment Variables
The following environment variables are required:VITE_AUTH0_DOMAIN- Your Auth0 tenant domainVITE_AUTH0_CLIENT_ID- Your Auth0 application client IDVITE_AUTH0_AUDIENCE- Your Auth0 API audience identifier
Authentication Flow
User Access
When a user attempts to access a protected route, the
AuthGuard component checks authentication status.Login Redirect
If not authenticated, the user is shown the Login component with a popup-based login flow.
Token Management
After successful authentication, Auth0 manages JWT tokens automatically, including refresh token rotation.
Login Component
The Login component provides a simple, centered login button that triggers Auth0’s popup authentication:src/components/Login.tsx
Protected Routes with AuthGuard
TheAuthGuard component protects routes by checking authentication status before rendering protected content:
src/components/AuthGuard.tsx
How It Works
- Loading State
- Unauthenticated
- Authenticated
- Error Handling
While Auth0 checks authentication status, a centered loading spinner is displayed:
Logout Functionality
Users can log out from the Dashboard or navigation drawer. The logout function redirects users back to the application origin:src/pages/Dashboard/Dashboard.tsx
The
returnTo parameter ensures users are redirected back to your application after logout. Make sure this URL is configured in your Auth0 application’s “Allowed Logout URLs”.Accessing User Information
The Auth0 hook provides access to user profile information:Security Best Practices
Token Management
Auth0 handles token refresh automatically. Access tokens are stored securely and never exposed in localStorage.
API Protection
All API calls include the JWT token in the Authorization header for server-side validation.
Popup Flow
Using popup authentication provides better UX than redirects while maintaining security.
Error Handling
Authentication errors are gracefully handled and displayed to users.
Next Steps
Dashboard
Learn about the dashboard UI and navigation
API Integration
Discover how API calls use JWT tokens
