Skip to content

Latest commit

 

History

History
238 lines (171 loc) · 4.59 KB

File metadata and controls

238 lines (171 loc) · 4.59 KB

AI Tutor - AI-Powered English Learning Platform

An interactive AI-powered English learning platform built with Next.js, TypeScript, Prisma, and OpenAI. AI Tutor provides personalized English tutoring through an intelligent chat interface.

NextJS TypeScript Prisma JWT Tailwind CSS OpenAI

💾 Database Setup

Schema

The application uses two main models:

User Model

model User {
  id            Int           @id @default(autoincrement())
  name          String
  email         String        @unique
  password      String
  goal          String?       # Learning goal
  level         String?       # Proficiency level
  createdAt     DateTime      @default(now())
  conversations Conversation[]
}

Conversation Model

model Conversation {
  id        Int      @id @default(autoincrement())
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId    Int
  messages  Json     # Stores chat history
  createdAt DateTime @default(now())
}

📡 API Documentation

Authentication Endpoints

POST /api/signup

Register a new user account.

Request Body:

{
  "name": "John Doe",
  "email": "john@example.com",
  "password": "password123"
}

Response (201):

{
  "code": 201,
  "message": "Successfully registered new account!"
}

Validation:

  • Name: Required, max 100 characters
  • Email: Valid email format required
  • Password: Minimum 6 characters

POST /api/login

Authenticate an existing user.

Request Body:

{
  "email": "john@example.com",
  "password": "password123"
}

Response (200):

{
  "code": 200,
  "message": "Successfully Logged in",
  "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}

Error Response (401):

{
  "code": 401,
  "error": "Invalid email or password!"
}

Chat Endpoints

GET /api/onboarding/chat

Retrieve conversation history for the authenticated user.

Headers:

x-user-id: {userId}

Response (200):

{
  "code": 200,
  "data": {
    "id": 1,
    "userId": 1,
    "messages": [
      {
        "role": "ai",
        "content": "Hi John! Welcome to JaPi. What's your English learning goal?",
        "date": "2025-10-30T10:00:00.000Z"
      }
    ],
    "createdAt": "2025-10-30T09:00:00.000Z"
  }
}

POST /api/onboarding/chat

Send a message and receive AI response.

Headers:

x-user-id: {userId}

Request Body:

{
  "message": "I want to improve my business English"
}

Response (200):

{
  "code": 200,
  "data": {
    "reply": "Great! Business English is very useful. What's your current level?",
    "goal": "improve business English",
    "level": ""
  }
}

AI Onboarding Flow:

  1. Step 1/2: Extract user's learning goal

    • AI asks: "What's your English learning goal?"
    • Stores goal, then asks about proficiency level
  2. Step 2/2: Extract proficiency level

    • AI asks: "What's your English level? (beginner/intermediate/advanced)"
    • Stores level, then starts first lesson
  3. Active Learning: Once profile complete, provides personalized tutoring

🔑 Authentication

JWT Token Flow

  1. Signup/Login: User provides credentials
  2. Token Generation: Server creates JWT with user ID and email
  3. Token Storage: Client stores token in cookies
  4. Protected Routes: Middleware validates token
  5. User Identification: Token payload extracted for API calls

🧪 Development

Scripts

# Development server
npm run dev

# Production build
npm run build

# Start production server
npm run start

# Lint code
npm run lint

Code Quality

  • ESLint: Code linting with Next.js config
  • TypeScript: Strict type checking
  • Zod: Runtime validation for API inputs

Environment Setup

Ensure all environment variables are configured in your deployment platform:

  • DATABASE_URL
  • JWT_SECRET
  • OPENAI_API_KEY
  • NEXT_PUBLIC_BASE_URL

Built with ❤️