|
| 1 | +# PromptQL Docs Authentication Setup |
| 2 | + |
| 3 | +This directory contains the mock authentication setup for the PromptQL documentation site using Hydra OAuth2 server. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +1. **Start the development environment:** |
| 8 | + ```bash |
| 9 | + yarn start |
| 10 | + ``` |
| 11 | + |
| 12 | +2. **Test the authentication flow:** |
| 13 | + - Visit http://localhost:3001 |
| 14 | + - Try to access any documentation page (e.g., http://localhost:3001/docs/quickstart/) |
| 15 | + - You should be redirected to the login page |
| 16 | + - Click "Sign In with Hasura Cloud" to start the OAuth flow |
| 17 | + |
| 18 | +3. **Test production build locally:** |
| 19 | + ```bash |
| 20 | + yarn build:local |
| 21 | + ``` |
| 22 | + |
| 23 | +## Architecture Overview |
| 24 | + |
| 25 | +### Components |
| 26 | + |
| 27 | +1. **Hydra OAuth2 Server** (Port 4444/4445) |
| 28 | + - Handles OAuth2 authorization flow |
| 29 | + - Admin API on port 4445, Public API on port 4444 |
| 30 | + |
| 31 | +2. **Login/Consent App** (Port 3000) |
| 32 | + - Mock login interface that simulates Hasura Cloud login |
| 33 | + - Handles user consent for OAuth2 flow |
| 34 | + |
| 35 | +3. **Docusaurus App** (Port 3001) |
| 36 | + - Main documentation site with integrated authentication |
| 37 | + |
| 38 | +### Authentication Flow |
| 39 | + |
| 40 | +1. User visits protected documentation page |
| 41 | +2. `Root.tsx` provides AuthContext to entire app |
| 42 | +3. `Layout.tsx` component checks authentication status |
| 43 | +4. If not authenticated, `ProtectedRoute.tsx` shows login prompt |
| 44 | +5. User clicks login β redirected to Hydra OAuth2 endpoint |
| 45 | +6. Hydra redirects to mock login app (port 3000) |
| 46 | +7. User "logs in" β redirected back to Hydra |
| 47 | +8. Hydra redirects to `/docs/callback` with authorization code |
| 48 | +9. `AuthContext.tsx` exchanges code for access token (with duplicate call protection) |
| 49 | +10. User info is fetched and stored in cookies |
| 50 | +11. User is redirected to original destination |
| 51 | + |
| 52 | +### Key Files |
| 53 | + |
| 54 | +- `src/contexts/AuthContext.tsx` - Main authentication logic with OAuth2 flow |
| 55 | +- `src/theme/Root.tsx` - Provides AuthContext to entire app |
| 56 | +- `src/theme/Layout/index.tsx` - Route protection logic and loading states |
| 57 | +- `src/components/ProtectedRoute.tsx` - Protected route wrapper with delayed loading |
| 58 | +- `src/pages/login.tsx` - Login page with brand styling |
| 59 | +- `src/pages/callback.tsx` - OAuth callback handler with duplicate call protection |
| 60 | +- `src/theme/Navbar/Content/index.tsx` - Navbar with conditional auth elements |
| 61 | +- `src/css/custom.css` - Authentication styles with brand colors and animations |
| 62 | + |
| 63 | +## Configuration |
| 64 | + |
| 65 | +### OAuth2 Settings |
| 66 | +- **Client ID:** `docusaurus-client` |
| 67 | +- **Redirect URI:** `http://localhost:3001/docs/callback` |
| 68 | +- **Scopes:** `openid email` |
| 69 | + |
| 70 | +### Public Routes |
| 71 | +The following routes are accessible without authentication: |
| 72 | +- `/` (landing page) |
| 73 | +- `/docs/` (docs landing page) |
| 74 | +- `/login` (login page) |
| 75 | +- `/docs/callback` (OAuth callback) |
| 76 | + |
| 77 | +All other routes require authentication. |
| 78 | + |
| 79 | +## Key Features |
| 80 | + |
| 81 | +### UI/UX Enhancements |
| 82 | +- **Brand Colors**: Uses PromptQL brand colors that adapt to light/dark themes |
| 83 | +- **Delayed Loading**: Loading animations only appear after 5 seconds to avoid visual noise |
| 84 | +- **Animated Dots**: Professional 3-dot loading animation using CSS keyframes |
| 85 | +- **Conditional Navbar**: Search and navigation elements hidden when unauthenticated |
| 86 | +- **Responsive Design**: Works across all device sizes |
| 87 | + |
| 88 | +### Technical Features |
| 89 | +- **Duplicate Call Protection**: Prevents React Strict Mode from causing "code already used" errors |
| 90 | +- **CORS Support**: Proper CORS configuration for browser-to-Hydra communication |
| 91 | +- **Session Persistence**: Authentication state persists across browser refreshes |
| 92 | +- **Error Handling**: Comprehensive error states with user-friendly messages |
| 93 | +- **Clean Architecture**: Centralized auth logic with proper separation of concerns |
| 94 | + |
| 95 | +## Development vs Production |
| 96 | + |
| 97 | +### Development (Current Setup) |
| 98 | +- Mock Hydra server with in-memory storage and CORS enabled |
| 99 | +- Mock login/consent interface configured as public client (no client secret) |
| 100 | +- User access check always returns `true` |
| 101 | +- Access tokens stored in browser cookies (`pql-docs-access`) with 1-day expiration; refresh tokens in (`pql-docs-refresh`) |
| 102 | +- Tokens are retrieved from cookies and sent as Authorization Bearer headers to GraphQL API |
| 103 | +- Delayed loading animations (5-second threshold) |
| 104 | +- Brand-consistent styling across light/dark themes |
| 105 | + |
| 106 | +### Production (Future) |
| 107 | +- Real Hydra endpoints: `https://oauth.pro.hasura.io/*` |
| 108 | +- Real Hasura Cloud login interface configured as public client |
| 109 | +- GraphQL API check against `ddn_promptql_enabled_users` table |
| 110 | +- Secure httpOnly cookies for token storage |
| 111 | +- Tokens sent as Authorization Bearer headers to GraphQL API |
| 112 | +- Proper error handling and security headers |
| 113 | + |
| 114 | +## Troubleshooting |
| 115 | + |
| 116 | +### Services won't start |
| 117 | +```bash |
| 118 | +# Check if ports are in use |
| 119 | +lsof -i :3000 -i :4444 -i :4445 |
| 120 | + |
| 121 | +# Stop existing containers |
| 122 | +docker compose -f auth/compose.yml down |
| 123 | + |
| 124 | +# Restart services |
| 125 | +./auth/start-auth-dev.sh |
| 126 | +``` |
| 127 | + |
| 128 | +### Authentication fails |
| 129 | +```bash |
| 130 | +# Check service logs |
| 131 | +docker compose -f auth/compose.yml logs -f |
| 132 | + |
| 133 | +# Verify client registration |
| 134 | +curl http://localhost:4445/admin/clients/docusaurus-client |
| 135 | +``` |
| 136 | + |
| 137 | +### Clear authentication state |
| 138 | +```bash |
| 139 | +# In browser console: |
| 140 | +document.cookie.split(";").forEach(function(c) { |
| 141 | + document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); |
| 142 | +}); |
| 143 | +``` |
| 144 | +
|
| 145 | +## Testing Checklist |
| 146 | +
|
| 147 | +- [ ] Unauthenticated users can access landing pages (`/` and `/docs/`) |
| 148 | +- [ ] Unauthenticated users redirected to login for protected pages |
| 149 | +- [ ] Login flow redirects to Hydra auth endpoint |
| 150 | +- [ ] Mock login interface appears and works correctly |
| 151 | +- [ ] Successful auth redirects to `/docs/callback` then to original destination |
| 152 | +- [ ] User email appears in navbar after authentication |
| 153 | +- [ ] Logout clears session and redirects to `/docs/index/` |
| 154 | +- [ ] Auth state persists across browser refresh |
| 155 | +- [ ] Loading animations only appear after 5-second delay |
| 156 | +- [ ] Search bar and navigation hidden when unauthenticated |
| 157 | +- [ ] Error handling works for failed auth attempts |
| 158 | +- [ ] No "authorization code already used" errors (duplicate call protection) |
| 159 | +
|
| 160 | +## Commands |
| 161 | +
|
| 162 | +```bash |
| 163 | +# Start development environment |
| 164 | +yarn start |
| 165 | + |
| 166 | +# Stop development services |
| 167 | +yarn dev:stop |
| 168 | + |
| 169 | +# Test production build locally |
| 170 | +yarn build:local |
| 171 | + |
| 172 | +# Manual service management (if needed) |
| 173 | +docker compose -f auth/compose.yml logs -f # View logs |
| 174 | +docker compose -f auth/compose.yml down # Stop services |
| 175 | +docker compose -f auth/compose.yml restart hydra # Restart Hydra |
| 176 | + |
| 177 | +# Check client configuration |
| 178 | +curl http://localhost:4445/admin/clients/docusaurus-client |
| 179 | +``` |
0 commit comments