Authentication
Login flows with JWT tokens and protected routes
Key Concepts
- JWT (JSON Web Tokens) for stateless authentication
- Protected routes - redirect if not authenticated
- Token storage (localStorage, cookies, httpOnly)
- Refresh tokens for extended sessions
- Role-based access control (RBAC)
Login to Continue
🔧 What This Demo Does
1. Simulated Authentication Flow
Implements a complete login/register system with client-side validation. Uses localStorage to persist JWT tokens and user data (in production, use httpOnly cookies for security). Demonstrates password hashing concepts and form validation patterns.
2. Protected Route Pattern
Shows how to protect content behind authentication. The "Protected" route checks for a valid token before displaying content. If no token exists, it automatically redirects to the login page - simulating Next.js middleware or route guards in production apps.
3. Token Management
Demonstrates JWT token lifecycle: generation on login, storage in localStorage, validation before accessing protected routes, and removal on logout. In production, tokens would be validated server-side with a secret key.
4. Client-Side Routing Simulation
Uses React state to simulate route changes between Login and Protected pages. Shows how authentication state affects navigation - users can't manually navigate to protected content without logging in first.
5. User Session Persistence
Maintains user session across page refreshes by checking localStorage on component mount. Automatically restores the logged-in state if a valid token exists, providing a seamless user experience.
🔄 How It Works:
- User enters credentials → Client validates format
- On submit → Generates fake JWT token + stores in localStorage
- User navigates to Protected route → Checks for token
- If token exists → Shows protected content
- If no token → Redirects to Login page
- Logout → Clears localStorage + resets state
⚠️ Production Notes:
- • Use httpOnly cookies instead of localStorage for security
- • Implement server-side token validation with secret keys
- • Add refresh token rotation for extended sessions
- • Use HTTPS only - never send tokens over HTTP
- • Implement CSRF protection for state-changing operations