Skip to content

Latest commit

 

History

History
181 lines (145 loc) · 6.14 KB

File metadata and controls

181 lines (145 loc) · 6.14 KB

Technical Summary

Overview

This is a full-stack shoe collection management application with JWT authentication, built with TypeScript, Node.js, Express, Prisma ORM, and React.

Architecture

Backend (Node.js + Express + TypeScript)

  • Framework: Express.js with TypeScript
  • Language: TypeScript for type safety and better developer experience
  • Database: SQLite with Prisma ORM
  • Authentication: JWT (JSON Web Tokens)
  • Password Security: bcryptjs for hashing
  • Type Safety: Full TypeScript coverage with strict mode

Frontend (React.js + TypeScript)

  • Framework: React with TypeScript
  • Language: TypeScript for type safety
  • HTTP Client: Axios with TypeScript types
  • State Management: React useState and useEffect with TypeScript
  • Routing: Client-side authentication logic
  • Type Safety: All components, services, and interfaces are typed

Data Flow

Authentication Flow

  1. User submits credentials (register/login)
  2. Backend validates and hashes password (bcryptjs)
  3. JWT token generated with user ID
  4. Token stored in localStorage
  5. Subsequent requests include token in Authorization header
  6. Middleware validates token on protected routes

CRUD Flow

  1. Frontend component triggers action (e.g., addShoe)
  2. API service method called (e.g., shoesAPI.addShoe)
  3. Axios sends HTTP request to Express backend
  4. Authentication middleware validates JWT token
  5. Controller extracts data and calls service layer
  6. Service layer interacts with Prisma database
  7. Response flows back through the stack
  8. UI updates reactively

Project Structure

Backend Layers

  • Routes (/routes): Define endpoints and mount middleware
  • Controllers (/controllers): Handle HTTP requests/responses
  • Services (/services): Business logic and database operations
  • Middleware (/middleware): Authentication validation
  • Prisma (/prisma): Database schema and migrations

Frontend Structure

  • Components (/components): TypeScript React components
    • Login.tsx: TypeScript authentication form with typed props
    • ShoeList.tsx: TypeScript component to display all shoes
    • AddShoeForm.tsx: TypeScript form to add new shoes
  • Services (/services): TypeScript API communication layer with typed interfaces
  • App.tsx: Main TypeScript application logic and state management

Security Features

  1. Password Hashing: bcryptjs with salt rounds (10)
  2. JWT Tokens: Signed with secret key, 1-hour expiration
  3. Protected Routes: Middleware checks for valid token
  4. CORS: Enabled for cross-origin requests
  5. Input Validation: Required fields on forms

Database Schema

User Table

- id (Int, auto-increment, primary key)
- username (String, unique)
- password (String, hashed)
- createdAt (DateTime, default now)

Shoe Table

- id (Int, auto-increment, primary key)
- name (String)
- brand (String)
- createdAt (DateTime, default now)

API Design

Endpoints

Authentication

  • POST /api/auth/register: Register new user

    • Body: { username, password }
    • Response: { message: "User registered successfully" }
  • POST /api/auth/login: Login user

    • Body: { username, password }
    • Response: { token: "jwt-token" }

Shoes (Protected - requires Bearer token)

  • GET /api/shoes: Get all shoes

    • Response: [{ id, name, brand, createdAt }, ...]
  • POST /api/shoes: Add new shoe

    • Body: { name, brand }
    • Response: { id, name, brand, createdAt }
  • DELETE /api/shoes/:id: Delete shoe by ID

    • Response: 204 No Content

Error Handling

Backend

  • Try-catch blocks in all async functions
  • HTTP status codes: 200, 201, 204, 401, 500
  • Error messages returned to client
  • Database errors caught and logged

Frontend

  • API error responses handled
  • User-friendly error messages displayed
  • Loading states for async operations
  • Form validation before submission

Key Decisions

  1. TypeScript Throughout: Both frontend and backend use TypeScript for type safety, better IDE support, and catch errors at compile time
  2. Separation of Concerns: Clear layering (routes → controllers → services)
  3. Modular Components: Each React component handles specific UI concern with typed props and interfaces
  4. Centralized API Service: Single axios instance with interceptors and TypeScript types
  5. Local Storage: Simple token persistence without cookies
  6. Prisma ORM: Type-safe database queries and migrations with TypeScript integration
  7. SQLite: Self-contained database for simplicity
  8. bcryptjs: Industry-standard password hashing
  9. CORS Middleware: Enable frontend-backend communication
  10. Strict TypeScript: Enabled strict mode for maximum type safety

Development Practices

  • Environment Variables: Sensitive data in .env files
  • Gitignore: Exclude node_modules, database, .env
  • Code Organization: Clean folder structure for scalability
  • Error Messages: Informative feedback to users
  • Loading States: UX best practices for async operations
  • Responsive Design: Modern CSS with gradients and cards

Testing Recommendations

  1. Test authentication flow (register → login)
  2. Test CRUD operations (add, view, delete shoes)
  3. Test error scenarios (invalid credentials, duplicate usernames)
  4. Test token expiration (logout after 1 hour)
  5. Test concurrent operations
  6. Test data persistence after page refresh

Future Enhancements

  • Add user-specific shoe collections (foreign keys)
  • Add update/edit functionality for shoes
  • Add search and filter capabilities
  • Add pagination for large datasets
  • Add user profile management
  • Add password reset functionality
  • Switch to PostgreSQL for production
  • Add Docker containerization
  • Add automated testing (Jest, Supertest)
  • Add CI/CD pipeline

Deployment Considerations

  1. Change JWT_SECRET to secure random string
  2. Use environment-specific .env files
  3. Configure CORS to allow production domain only
  4. Use production database (PostgreSQL)
  5. Build React app: npm run build
  6. Serve static files from Express or CDN
  7. Set up reverse proxy (nginx)
  8. Enable HTTPS
  9. Monitor logs and errors
  10. Set up database backups