agent_crew
agent_crew copied to clipboard
feat: Auth-aware routing + JWT management + protected routes
Summary
Implement auth-aware routing that protects all app routes when auth is enabled, manages JWT lifecycle, and provides a seamless experience for both noop and local providers.
Context
The routing layer needs to conditionally protect routes based on the auth config. When AUTH_PROVIDER=noop, everything works as today. When AUTH_PROVIDER=local, unauthenticated users are redirected to login.
Implementation
Auth Context Provider (src/context/AuthContext.tsx)
interface AuthContextType {
user: User | null;
organization: Organization | null;
authConfig: AuthConfig | null;
isLoading: boolean;
isAuthenticated: boolean;
login: (credentials: LoginRequest) => Promise<void>;
register: (data: RegisterRequest) => Promise<void>;
logout: () => void;
}
App Initialization Flow
App mounts
→ GET /api/auth/config
├─ provider=noop
│ → Set isAuthenticated=true, load default user
│ → Render app (no auth UI)
└─ provider=local
├─ has valid access_token in localStorage
│ → GET /api/auth/me
│ ├─ success → set user/org, render app
│ └─ 401 → try refresh → if fail → redirect /login
└─ no token
→ redirect /login
Protected Route Component
function ProtectedRoute({ children }) {
const { isAuthenticated, isLoading, authConfig } = useAuth();
if (isLoading) return <LoadingSkeleton />;
if (authConfig?.provider === 'noop') return children;
if (!isAuthenticated) return <Navigate to="/login" />;
return children;
}
Route Structure Update (App.tsx)
<Routes>
{/* Public routes */}
<Route path="/login" element={<LoginPage />} />
<Route path="/register" element={<RegisterPage />} />
<Route path="/invite/:token" element={<InvitePage />} />
{/* Protected routes */}
<Route element={<ProtectedRoute><Layout /></ProtectedRoute>}>
<Route path="/" element={<TeamsListPage />} />
<Route path="/teams/new" element={<TeamBuilderPage />} />
{/* ... all existing routes ... */}
<Route path="/settings/profile" element={<UserProfilePage />} />
<Route path="/settings/organization" element={<OrgSettingsPage />} />
</Route>
</Routes>
Layout Updates
When auth is active (provider !== 'noop'):
- Show user name/email in sidebar footer
- Show org name in sidebar header
- Add "Logout" button
- Add links to Profile and Organization settings
When auth is noop:
- Layout stays exactly as it is today
- No user info shown, no logout button
JWT Lifecycle
- Storage:
localStoragekeys:access_token,refresh_token - Injection: All API requests include
Authorization: Bearer <token> - Refresh: On 401, call
/api/auth/refreshwith refresh token - Logout: Clear tokens, redirect to
/login - Expiry check: Optional - decode JWT client-side to check exp before each request
Acceptance Criteria
- [ ] AuthContext provider with user/org state
- [ ] Auth config fetch on app mount
- [ ] Protected route component
- [ ] Noop provider: app works identically to current behavior
- [ ] Local provider: redirects to login when unauthenticated
- [ ] JWT storage in localStorage
- [ ] Authorization header on all API requests
- [ ] Automatic token refresh on 401
- [ ] Logout clears tokens and redirects
- [ ] Layout shows user/org info when auth active
- [ ] Login/Register pages redirect to dashboard if already authenticated
- [ ] Full E2E test: noop mode (no auth UI shown)
- [ ] Full E2E test: local mode (login → dashboard → logout)
- [ ] Full E2E test: token refresh flow
Dependencies
- Depends on: #35 (auth types + API client), #36 (login/register pages)
Related Issues
Part of Auth & Multi-tenancy epic (Phase 1). This is the integration issue that ties all frontend auth work together.
Completed in v0.3.0 release (commit 8ca8e5c). All auth UI features implemented and tested.