A full-stack Next.js application that allows users to generate React components through natural language conversation with AI. Built with MongoDB, JWT authentication, and the AI SDK.
- Authentication & Persistence: Secure JWT-based auth with MongoDB
- Conversational UI: Chat interface for component generation
- Live Preview: Real-time component rendering in iframe sandbox
- Code Inspection: Syntax-highlighted JSX/CSS code tabs
- Export Functionality: Copy and download generated components
- Session Management: Persistent chat history and auto-save
- Frontend: Next.js 14 with React 18, Tailwind CSS, shadcn/ui
- Backend: Next.js API routes with MongoDB integration
- AI Integration: AI SDK with OpenAI GPT-4o-mini
- Authentication: JWT tokens with bcrypt password hashing
- State Management: React hooks with localStorage persistence
- Code Rendering: Secure iframe sandbox with Babel transpilation
- Frontend: React, Next.js, TypeScript, Tailwind CSS
- Backend: Node.js, Express (via Next.js API routes)
- Database: MongoDB with Mongoose ODM
- Authentication: JWT tokens, bcryptjs
- AI: AI SDK with OpenAI integration
- UI Components: shadcn/ui, Radix UI primitives
- Styling: Tailwind CSS with responsive design
- Code Highlighting: react-syntax-highlighter with Prism
- Node.js 18+ and npm
- MongoDB database (local or cloud)
- OpenAI API key
-
Clone the repository ```bash git clone cd ai-component-generator ```
-
Install dependencies ```bash npm install ```
-
Set up environment variables ```bash cp .env.example .env.local ```
Update
.env.localwith your values: ```env MONGODB_URI=your mongodb url JWT_SECRET=your-super-secret-jwt-key-here OPENAI_API_KEY=your-openai-api-key-here ``` -
Start MongoDB ```bash
mongod ```
-
Run the development server ```bash npm run dev ```
-
Open your browser Navigate to http://localhost:3000
``` app/ ├── page.tsx # Landing page ├── auth/ │ ├── login/page.tsx # Login page │ └── signup/page.tsx # Signup page ├── dashboard/page.tsx # User dashboard └── playground/[sessionId]/ # Component generator └── page.tsx ```
``` app/api/ ├── auth/ │ ├── login/route.ts # User authentication │ └── signup/route.ts # User registration └── sessions/ ├── route.ts # Session CRUD operations ├── [sessionId]/route.ts # Individual session └── [sessionId]/messages/route.ts # Chat messages ```
```javascript // User Model { name: String, email: String (unique), password: String (hashed), timestamps: true }
// Session Model { userId: ObjectId, title: String, messages: [{ role: 'user' | 'assistant', content: String, timestamp: Date }], generatedCode: { jsx: String, css: String }, timestamps: true } ```
- User registers/logs in with email/password
- Server validates credentials and returns JWT token
- Client stores token in localStorage
- All API requests include Authorization header
- Server validates JWT on protected routes
- User sends natural language prompt
- System builds conversation context
- AI SDK generates React component code
- Code is extracted and stored in session
- Live preview updates via iframe sandbox
- Client State: React hooks + localStorage for auth
- Server State: MongoDB for persistent data
- Auto-save: Triggered after each chat interaction
- Session Resume: Full state restoration on login
- Password hashing with bcrypt (12 rounds)
- JWT tokens with 7-day expiration
- Input validation and sanitization
- Iframe sandbox for code execution
- CORS protection on API routes
- Sign up/Login to access the platform
- Create New Session from dashboard
- Describe your component in natural language:
- "Create a responsive login form with email and password fields"
- "Build a product card with image, title, price, and buy button"
- "Make a navigation bar with logo and menu items"
- Refine existing components: "Make the button larger and blue"
- Add functionality: "Add form validation with error messages"
- Modify styling: "Use a dark theme with rounded corners"
- Copy: Click copy button to get JSX code
- Download: Get complete component files as ZIP
- Preview: See live component rendering
-
Push to GitHub ```bash git add . git commit -m "Initial commit" git push origin main ```
-
Deploy to Vercel
- Connect GitHub repository
- Add environment variables
- Deploy automatically
-
Set up MongoDB Atlas
- Create cluster on MongoDB Atlas
- Update MONGODB_URI in Vercel environment
```env MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/componentai JWT_SECRET=production-secret-key OPENAI_API_KEY=your-openai-api-key NEXTAUTH_URL=https://your-domain.vercel.app ```
- ✅ Auth & Backend (10 pts): JWT sessions, password hashing, MongoDB
- ✅ State Management (15 pts): Session persistence, auto-save, reload
- ✅ AI Integration (20 pts): OpenAI integration, prompt handling
- ✅ Micro-Frontend (10 pts): Iframe sandbox, live preview
- ✅ Code Editor (10 pts): Syntax highlighting, export functionality
- ✅ Iterative Workflow (10 pts): Chat interface, component refinement
- ✅ Persistence (10 pts): Auto-save, session resume
- ✅ Polish & Accessibility (10 pts): Responsive design, loading states
- 🔄 Interactive Property Editor: Click-to-edit component properties
- 🔄 Chat-Driven Overrides: Target specific elements via chat
- Fork the repository
- Create feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
For issues and questions:
- Create GitHub issue
- Check documentation
- Review code comments
Live Demo: Deploy your own instance GitHub: Repository Link