MATIH Platform is in active MVP development. Documentation reflects current implementation status.
15. Workbench Architecture
Shared Library Deep Dive
Authentication Components

Authentication Components

The shared authentication components at frontend/shared/src/components/Auth/ provide a complete authentication UI layer including session verification, route protection, login/registration forms, permission-based access control, and session management. All components use httpOnly cookie-based authentication.


AuthGuard

File: frontend/shared/src/components/Auth/AuthGuard.tsx

The primary authentication wrapper that verifies user sessions before rendering protected content.

Props Interface

interface AuthGuardProps {
  children: ReactNode;
  authCheckEndpoint?: string;    // Default: '/api/v1/auth/me'
  loginUrl?: string;             // Default: '/login'
  includeReturnUrl?: boolean;    // Default: true
  returnUrlParam?: string;       // Default: 'returnTo'
  loadingComponent?: ReactNode;
  onAuthError?: (error: Error) => void;
  onAuthenticated?: (user: AuthGuardUser) => void;
}

User Context

interface AuthGuardUser {
  id: string;
  email: string;
  firstName?: string;
  lastName?: string;
  avatar?: string;
  tenantId?: string;
  roles: string[];
  permissions: string[];
}
 
interface AuthGuardContextType {
  user: AuthGuardUser | null;
  isAuthenticated: boolean;
  isLoading: boolean;
  error: string | null;
  logout: () => Promise<void>;
  refreshSession: () => Promise<void>;
}

Usage

<AuthGuard
  loginUrl="https://auth.matih.ai/login"
  onAuthenticated={(user) => initializeAnalytics(user)}
>
  <App />
</AuthGuard>

Hook

const { user, isAuthenticated, logout, refreshSession } = useAuthGuard();

HOC

const ProtectedPage = withAuthGuard(DashboardPage, {
  loginUrl: '/login',
  onAuthError: (err) => trackError(err),
});

Additional Auth Components

ComponentFilePurpose
LoginFormAuth/LoginForm.tsxEmail/password login with validation
RegisterFormAuth/RegisterForm.tsxNew user registration
ProtectedRouteAuth/ProtectedRoute.tsxRoute-level protection with React Router
PermissionGuardAuth/PermissionGuard.tsxRenders children only if user has required permissions
RoleBasedAccessAuth/RoleBasedAccess.tsxRole-based content rendering
SessionManagerAuth/SessionManager.tsxSession timeout, refresh, and activity tracking
SecuritySettingsAuth/SecuritySettings.tsxPassword change, MFA setup
ProfileManagerAuth/ProfileManager.tsxUser profile editing
APIKeyManagerAuth/APIKeyManager.tsxAPI key generation and management

Authentication Flow

1. User navigates to protected route
2. AuthGuard renders loading spinner
3. GET /api/v1/auth/me with credentials: 'include'
4a. 200 OK -> Parse user, provide context, render children
4b. 401/403 -> Redirect to loginUrl with returnTo parameter
4c. Error -> Call onAuthError, redirect to login

The loginUrl supports both relative paths (/login) and absolute URLs (https://auth.matih.ai/login), with return URL handling adapted for each case.