Playerss

πŸ† Quest Tracker API v2.0 - Extended

A production-ready REST API that extends Template 11 with JWT authentication, MongoDB integration, and social features. Transform your gamified quest system into a scalable, multi-user application.

πŸš€ What’s New in v2.0

This extended version transforms the original Template 11 into a production-ready API:

✨ New Features

πŸ“ Project Structure

quest-tracker-api-extended/
β”œβ”€β”€ package.json                 # Updated dependencies
β”œβ”€β”€ server.js                    # Enhanced server with JWT & DB
β”œβ”€β”€ .env.example                 # Environment configuration template
β”œβ”€β”€ README.md                    # This file
β”œβ”€β”€ database/
β”‚   β”œβ”€β”€ connection.js           # MongoDB connection setup
β”‚   └── models/
β”‚       β”œβ”€β”€ User.js             # User model with gamification
β”‚       β”œβ”€β”€ Quest.js            # Quest model with sharing
β”‚       └── Achievement.js      # Achievement definitions
β”œβ”€β”€ middleware/
β”‚   β”œβ”€β”€ auth.js                 # JWT + legacy API key auth
β”‚   └── validation.js           # Input validation (existing)
β”œβ”€β”€ routes/
β”‚   β”œβ”€β”€ auth.js                 # Authentication endpoints
β”‚   β”œβ”€β”€ quests.js               # Quest CRUD with ownership
β”‚   β”œβ”€β”€ players.js              # User profiles & leaderboards
β”‚   β”œβ”€β”€ categories.js           # Quest categories (existing)
β”‚   └── social.js               # Social features & discovery
β”œβ”€β”€ utils/
β”‚   └── jwt.js                  # JWT token utilities
β”œβ”€β”€ database/migrations/
β”‚   └── seed.js                 # Database seeding script
└── docs/                       # Documentation

πŸ› οΈ Setup Instructions

Prerequisites

1. Clone and Install

# Navigate to the extended project
cd quest-tracker-api-extended

# Install dependencies
npm install

2. Environment Configuration

# Copy environment template
cp .env.example .env

# Edit .env with your settings

Required Environment Variables:

# Server
PORT=3000
NODE_ENV=development

# JWT
JWT_SECRET=your-super-secret-jwt-key-change-in-production
JWT_EXPIRES_IN=7d

# Database
DATABASE_URL=mongodb://localhost:27017/quest-tracker
# OR for MongoDB Atlas:
# DATABASE_URL=mongodb+srv://username:password@cluster.mongodb.net/quest-tracker

# CORS
CORS_ORIGIN=http://localhost:3000,http://localhost:3001

# Legacy API Keys (comma-separated)
LEGACY_API_KEYS=demo_key_12345,test_key_67890,student_key_abcde

3. Database Setup

# Ensure MongoDB is running
# For local MongoDB:
mongod

# Seed the database with sample data
npm run db:seed

4. Start the Server

# Development mode (with auto-reload)
npm run dev

# Production mode
npm start

The API will be available at http://localhost:3000

πŸ“– API Documentation

πŸ” Authentication

The API supports two authentication methods:

# 1. Register a new user
curl -X POST http://localhost:3000/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "username": "demo",
    "email": "demo@example.com",
    "password": "Password123",
    "displayName": "Demo User"
  }'

# 2. Login to get token
curl -X POST http://localhost:3000/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "identifier": "demo",
    "password": "Password123"
  }'

# 3. Use token in subsequent requests
curl http://localhost:3000/api/quests \
  -H "Authorization: Bearer <your-jwt-token>"

Legacy API Key (Backward Compatible)

curl http://localhost:3000/api/quests \
  -H "X-API-Key: demo_key_12345"

🎯 Core Endpoints

Authentication

| Method | Endpoint | Description | Auth | |——–|β€”β€”β€”-|β€”β€”β€”β€”-|β€”β€”| | POST | /api/auth/register | Register new user | Public | | POST | /api/auth/login | Login and get JWT | Public | | POST | /api/auth/refresh | Refresh access token | Public | | GET | /api/auth/me | Get current user | JWT |

Quests

| Method | Endpoint | Description | Auth | |——–|β€”β€”β€”-|β€”β€”β€”β€”-|β€”β€”| | GET | /api/quests | List user’s quests | JWT/API Key | | POST | /api/quests | Create new quest | JWT | | GET | /api/quests/:id | Get quest details | JWT/API Key | | PUT | /api/quests/:id | Update quest | JWT (owner) | | DELETE | /api/quests/:id | Soft delete quest | JWT (owner) | | POST | /api/quests/:id/complete | Complete quest | JWT (owner) | | POST | /api/quests/:id/share | Share quest | JWT (owner) | | POST | /api/quests/:id/clone | Clone shared quest | JWT |

Players & Leaderboards

| Method | Endpoint | Description | Auth | |——–|β€”β€”β€”-|β€”β€”β€”β€”-|β€”β€”| | GET | /api/players | Leaderboard | JWT/API Key | | GET | /api/players/:id | Player profile | JWT/API Key | | GET | /api/players/me | Current user profile | JWT | | PUT | /api/players/me | Update profile | JWT | | GET | /api/players/me/achievements | User achievements | JWT | | POST | /api/players/:id/follow | Follow/unfollow | JWT |

Social & Discovery

| Method | Endpoint | Description | Auth | |——–|β€”β€”β€”-|β€”β€”β€”β€”-|β€”β€”| | GET | /api/quests/shared/public | Browse public quests | Public | | GET | /api/quests/templates | Quest templates | Public | | GET | /api/discover/trending | Trending quests | Public | | GET | /api/discover/recommended | Personalized | JWT | | GET | /api/social/feed | Activity feed | JWT | | POST | /api/social/quests/:id/like | Like/unlike quest | JWT |

πŸ“Š Response Format

All responses follow a consistent format:

{
  "success": true,
  "data": {
    // Response data here
  },
  "message": "Human readable message",
  "meta": {
    // Pagination, filters, etc.
  }
}

πŸ§ͺ Testing

Quick Test Commands

# Check API status
curl http://localhost:3000/api/status

# Test with legacy API key
curl http://localhost:3000/api/quests?api_key=demo_key_12345

# Register new user
curl -X POST http://localhost:3000/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "username": "testuser",
    "email": "test@example.com",
    "password": "TestPass123",
    "displayName": "Test User"
  }'

# Login
curl -X POST http://localhost:3000/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{"identifier":"demo","password":"Password123"}'

# Create a quest (with JWT)
curl -X POST http://localhost:3000/api/quests \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <token>" \
  -d '{
    "title": "Learn Node.js",
    "description": "Complete Node.js tutorial",
    "category": "learning",
    "priority": "high",
    "difficulty": "medium"
  }'

πŸ—„οΈ Database Schema

User Model

{
  username: String,           // Unique, required
  email: String,              // Unique, required
  password: String,           // Hashed with bcrypt
  displayName: String,
  level: Number,              // Gamification level
  totalXP: Number,
  currentStreak: Number,
  achievements: Array,
  preferences: {
    categories: Array,
    difficulty: String,
    theme: String,
    privacy: Object
  },
  social: {
    followers: Array,
    following: Array
  },
  stats: {
    totalQuestsStarted: Number,
    totalQuestsCompleted: Number
  }
}

Quest Model

{
  title: String,
  description: String,
  owner: ObjectId,            // Reference to User
  status: String,             // pending, in_progress, completed
  category: String,
  priority: String,
  difficulty: String,
  xpReward: Number,           // Auto-calculated
  deadline: Date,
  tags: Array,
  sharing: {
    isPublic: Boolean,
    isTemplate: Boolean,
    likes: Array,
    clones: Number
  },
  completion: {
    completedAt: Date,
    totalXPEarned: Number
  },
  isDeleted: Boolean          // Soft delete
}

πŸš€ Deployment

Environment Variables for Production

NODE_ENV=production
PORT=3000
JWT_SECRET=your-production-secret-key
DATABASE_URL=mongodb+srv://prod-user:password@cluster.mongodb.net/quest-tracker
CORS_ORIGIN=https://your-frontend-domain.com
  1. Create account at railway.app
  2. Connect your GitHub repository
  3. Add MongoDB database service
  4. Configure environment variables
  5. Deploy with automatic CI/CD

Deploy to Render

  1. Create account at render.com
  2. Create new Web Service
  3. Connect GitHub repository
  4. Add MongoDB add-on or external database
  5. Configure environment variables

πŸ“š Key Features Explained

JWT Authentication Flow

  1. User registers/logs in β†’ receives access token + refresh token
  2. Include Authorization: Bearer <token> header in requests
  3. Token expires after configured time (default 7 days)
  4. Use refresh token to get new access token

Quest Ownership

Quest Sharing

Gamification System

πŸ”’ Security Features

πŸ› Troubleshooting

Common Issues

Database Connection Failed

# Check MongoDB is running
mongod --version

# Verify connection string in .env
DATABASE_URL=mongodb://localhost:27017/quest-tracker

JWT Token Invalid

Cannot Create Quests

Port Already in Use

# Kill process on port 3000
npx kill-port 3000

# Or change PORT in .env
PORT=3001

πŸ“„ License

This project extends Template 11 from the W3.5 curriculum.

🀝 Contributing

This is a student project for learning purposes. Feel free to extend it with:

πŸ“ž Support

For issues or questions:

  1. Check the API docs at /api/docs when server is running
  2. Review the test commands in this README
  3. Check server logs for error messages

Built with ❀️ extending Template 11 Version 2.0.0