This guide outlines how to initialize and manage the CharAI frontend using Vite, React, TypeScript, and Material UI.
# Install dependencies npm install # or if using yarn yarn install # Start development server npm run dev
# Create new Vite project with React and TypeScript npm create vite@latest charai-frontend -- --template react-ts # Navigate to project directory cd charai-frontend # Install core dependencies npm install # Windows/Linux/macOS: same command
# Start the development server npm run dev # Build for production npm run build # Preview production build npm run preview
frontend/
├── src/
│ ├── components/ # Reusable UI components (Header, ProtectedRoute, PublicRoute, AppRoutes)
│ ├── pages/ # Route-level components (LoginPage, SignupPage, HomePage, App)
│ ├── services/ # API client and auth service (authService.ts)
│ ├── contexts/ # React context for state management (AuthContext)
│ ├── types/ # TypeScript type definitions (auth.ts)
│ ├── utils/ # Utility functions
│ ├── App.tsx # Root component
│ ├── main.tsx # Entry point
│ └── index.css # Global styles
├── public/ # Static assets
├── index.html # HTML template
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
├── vite.config.ts # Vite configuration
└── README.md # This file
The frontend integrates with Django REST Framework using TokenAuthentication and SessionAuthentication:
- Token Storage: Auth tokens are stored in
localStorageunder the keyauthToken - Session Support: Requests include
credentials: 'include'to support session-based auth - Protected Routes: Routes are guarded by
ProtectedRouteandPublicRoutecomponents - Auth Context: Centralized auth state via
AuthContext(provideslogin,register,logout,checkAuth,isAuthenticated,user,isLoading) - Error Handling: Errors from auth attempts are logged to console only; no UI error messages are displayed
- User submits login/signup form
- Frontend calls
authService.login()orauthService.register() - Backend validates and returns token (+ user data)
- Frontend stores token locally
- Subsequent requests include
Authorization: Token <token>header - On logout, token is cleared and user is redirected to login
src/services/authService.ts- API calls and token managementsrc/contexts/AuthContext.tsx- Auth state and methodssrc/types/auth.ts- TypeScript types for authsrc/components/ProtectedRoute.tsx- Guards authenticated pagessrc/components/PublicRoute.tsx- Prevents logged-in users from seeing login/signupsrc/pages/LoginPage.tsx- Login formsrc/pages/SignupPage.tsx- Registration formsrc/pages/HomePage.tsx- Protected home page (shown only to authenticated users)
# Type check and build npm run build # Lint the codebase npm run lint
The following scripts are available in package.json:
{
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
}
}To capture current dependencies for other developers:
# Create package.json.lock (npm) npm install # or yarn.lock (yarn) yarn install
Commit both package.json and the lock file to version control.
- Package manager:
npm - Framework: React 18+ with TypeScript
- UI Library: Material UI (MUI)
- Routing: React Router v6
- Package manager:
npm - Project root:
src/ - Main commands:
npm run dev- start development servernpm run build- build for production and type checknpm run preview- preview production buildnpm run lint- lint codebase
- Configuration:
- TypeScript:
tsconfig.json - Vite:
vite.config.ts - Environment:
.env
- TypeScript:
-
Install dependencies:
npm install
-
Ensure backend is running:
# In the backend directory python manage.py runserver -
Start the dev server:
npm run dev
-
Open the app:
- Navigate to configured frontend server URL
- Sign up or log in to see the protected home page