Skip to content

Nilesh091/Component-generation-backend

Repository files navigation

Component Generator Server API

A Node.js/Express server API for managing user authentication and component generation sessions.

Features

  • πŸ” 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

Tech Stack

  • Runtime: Node.js
  • Framework: Express.js
  • Database: MongoDB with Mongoose ODM
  • Authentication: JWT (JSON Web Tokens)
  • Password Hashing: bcryptjs
  • Environment: dotenv

Prerequisites

  • Node.js (v14 or higher)
  • MongoDB database
  • npm or yarn package manager

Installation

  1. Clone the repository:
git clone <repository-url>
cd server
  1. Install dependencies:
npm install
  1. Create a .env file 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
  1. Start the development server:
npm run dev

API Endpoints

Authentication

POST /api/auth/signup

Create 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"
  }
}

POST /api/auth/login

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 /api/auth/profile

Get current user profile (requires authentication).

Headers:

Authorization: Bearer <jwt-token>

PUT /api/auth/profile

Update user profile (requires authentication).

Headers:

Authorization: Bearer <jwt-token>

Request Body:

{
  "name": "Updated Name",
  "email": "[email protected]"
}

Sessions

GET /api/sessions

Get all sessions for authenticated user.

Headers:

Authorization: Bearer <jwt-token>

POST /api/sessions

Create a new session.

Headers:

Authorization: Bearer <jwt-token>

Request Body:

{
  "title": "My Component Session",
  "chatHistory": [],
  "componentCode": {
    "jsx": "",
    "css": ""
  },
  "editorState": {}
}

GET /api/sessions/:id

Get a specific session by ID.

Headers:

Authorization: Bearer <jwt-token>

PUT /api/sessions/:id

Update a session.

Headers:

Authorization: Bearer <jwt-token>

DELETE /api/sessions/:id

Delete a session.

Headers:

Authorization: Bearer <jwt-token>

POST /api/sessions/:id/messages

Add a message to session chat history.

Headers:

Authorization: Bearer <jwt-token>

Request Body:

{
  "sender": "user",
  "message": "Create a button component"
}

PUT /api/sessions/:id/component

Update component code.

Headers:

Authorization: Bearer <jwt-token>

Request Body:

{
  "jsx": "<button>Click me</button>",
  "css": "button { background: blue; }"
}

Error Responses

All endpoints return consistent error responses:

{
  "error": "Error message description"
}

Common HTTP status codes:

  • 200 - Success
  • 201 - Created
  • 400 - Bad Request
  • 401 - Unauthorized
  • 403 - Forbidden
  • 404 - Not Found
  • 409 - Conflict
  • 500 - Internal Server Error

Database Schema

User Model

{
  email: String (required, unique),
  passwordHash: String,
  name: String (required),
  provider: String (enum: ['local', 'google', 'github']),
  oauthId: String,
  timestamps: true
}

Session Model

{
  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
}

Security Features

  • 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

Development

Available Scripts

  • npm start - Start production server
  • npm run dev - Start development server with nodemon
  • npm test - Run tests (not implemented yet)

Environment Variables

Variable Description Default
PORT Server port 5000
NODE_ENV Environment development
MONGO_URI MongoDB connection string Required
JWT_SECRET JWT signing secret Required

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

License

ISC License

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published