A Node.js/Express server API for managing user authentication and component generation sessions.
- π User authentication with JWT tokens
- π Session management for component generation
- π¬ Chat history tracking
- π¨ Component code storage (JSX/CSS)
- π Secure password hashing with bcrypt
- π‘οΈ Input validation and error handling
- π MongoDB database integration
- Runtime: Node.js
- Framework: Express.js
- Database: MongoDB with Mongoose ODM
- Authentication: JWT (JSON Web Tokens)
- Password Hashing: bcryptjs
- Environment: dotenv
- Node.js (v14 or higher)
- MongoDB database
- npm or yarn package manager
- Clone the repository:
git clone <repository-url>
cd server- Install dependencies:
npm install- Create a
.envfile in the root directory:
# Server Configuration
PORT=5000
NODE_ENV=development
# Database Configuration
MONGO_URI=mongodb://localhost:27017/component-generator
# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key-here- Start the development server:
npm run devCreate a new user account.
Request Body:
{
"name": "John Doe",
"email": "[email protected]",
"password": "password123"
}Response:
{
"message": "User created successfully",
"token": "jwt-token-here",
"user": {
"_id": "user-id",
"name": "John Doe",
"email": "[email protected]",
"provider": "local",
"createdAt": "2024-01-01T00:00:00.000Z"
}
}Authenticate existing user.
Request Body:
{
"email": "[email protected]",
"password": "password123"
}Response:
{
"message": "Login successful",
"token": "jwt-token-here",
"user": {
"_id": "user-id",
"name": "John Doe",
"email": "[email protected]",
"provider": "local",
"createdAt": "2024-01-01T00:00:00.000Z"
}
}Get current user profile (requires authentication).
Headers:
Authorization: Bearer <jwt-token>
Update user profile (requires authentication).
Headers:
Authorization: Bearer <jwt-token>
Request Body:
{
"name": "Updated Name",
"email": "[email protected]"
}Get all sessions for authenticated user.
Headers:
Authorization: Bearer <jwt-token>
Create a new session.
Headers:
Authorization: Bearer <jwt-token>
Request Body:
{
"title": "My Component Session",
"chatHistory": [],
"componentCode": {
"jsx": "",
"css": ""
},
"editorState": {}
}Get a specific session by ID.
Headers:
Authorization: Bearer <jwt-token>
Update a session.
Headers:
Authorization: Bearer <jwt-token>
Delete a session.
Headers:
Authorization: Bearer <jwt-token>
Add a message to session chat history.
Headers:
Authorization: Bearer <jwt-token>
Request Body:
{
"sender": "user",
"message": "Create a button component"
}Update component code.
Headers:
Authorization: Bearer <jwt-token>
Request Body:
{
"jsx": "<button>Click me</button>",
"css": "button { background: blue; }"
}All endpoints return consistent error responses:
{
"error": "Error message description"
}Common HTTP status codes:
200- Success201- Created400- Bad Request401- Unauthorized403- Forbidden404- Not Found409- Conflict500- Internal Server Error
{
email: String (required, unique),
passwordHash: String,
name: String (required),
provider: String (enum: ['local', 'google', 'github']),
oauthId: String,
timestamps: true
}{
userId: ObjectId (ref: 'User', required),
title: String,
chatHistory: [{
sender: String (enum: ['user', 'ai']),
message: String,
timestamp: Date
}],
componentCode: {
jsx: String,
css: String
},
editorState: Object,
timestamps: true
}- Password hashing with bcrypt (12 salt rounds)
- JWT token authentication
- Input validation and sanitization
- CORS configuration
- Request size limits
- Error handling without exposing sensitive information
npm start- Start production servernpm run dev- Start development server with nodemonnpm test- Run tests (not implemented yet)
| Variable | Description | Default |
|---|---|---|
PORT |
Server port | 5000 |
NODE_ENV |
Environment | development |
MONGO_URI |
MongoDB connection string | Required |
JWT_SECRET |
JWT signing secret | Required |
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
ISC License