task-management-system

This repository contains the backend code for a Task Management System built using Node.js and Express.js. The system is designed to manage tasks efficiently with robust security features, including JWT-based authentication and role-based access control. The backend also leverages MongoDB for data storage and includes a comprehensive API .

Stars
0
Committers
2

Task Management API

Overview

This API provides a task management system with role-based access control. Users can manage tasks, with administrators having the ability to perform CRUD operations. The API includes user authentication, task management, and role-based permissions.

Features

  • User Authentication:
    • Register and log in users with JWT-based authentication.
    • Secure session management with token handling.
  • Task Management:
    • Create, read, update, and delete tasks.
    • Task details include title, description, priority, status, and assigned user.
  • Role-Based Access Control:
    • Only users with the role admin can perform CRUD operations on tasks.
    • Regular users can view tasks but cannot modify them.
  • Error Handling:
    • Proper error responses for invalid requests and unauthorized access.
  • Sample Data:
    • Includes example requests and responses for various endpoints.

Backend Endpoints

Auth Routes

  • POST /api/auth/register

    • Register a new user.
    • Request body:
      {
       
        "email": "[email protected]",
       "username": "John Doe",
        "password": "password123"
      
      }
      
    • Response:
      {
        "message": "Register successful",
        "user": {
          "_id": "userId",
          "name": "John Doe",
          "email": "[email protected]"
          
        }
      }
      
  • POST /api/auth/login

    • Log in an existing user.
    • Request body:
      {
        "email": "[email protected]",
        "password": "password123"
      }
      
    • Response:
      {
        "accessToken": "jwtToken",
        "refreshToken": "refreshToken",
        "user": {
          "_id": "userId",
          "name": "John Doe",
          "email": "[email protected]",
          "role": "admin"
        }
      }
      
  • POST /api/auth/refresh-token

    • Refresh the access token using a valid refresh token.
    • Request body:
      {
        "refreshToken": "refreshToken"
      }
      
    • Response:
      {
        "accessToken": "newAccessToken"
      }
      
  • POST /api/auth/logout

    • Invalidate the refresh token to log out a user.
    • Request body:
      {
        "refreshToken": "refreshToken"
      }
      
    • Response:
      {
        "message": "User logged out successfully"
      }
      

Task Routes

  • POST /api/tasks

    • Create a new task. Only accessible to users with the role admin.
    • Request body:
      {
        "title": "Complete project report",
        "description": "Finish the report and submit it by the end of the week.",
        "priority": "high",
        "status": "pending",
        "assignedTo": "userId" // Optional, user ID to whom the task is assigned.
      }
      
    • Response:
      {
        "title": "Complete project report",
        "description": "Finish the report and submit it by the end of the week.",
        "priority": "high",
        "status": "pending",
        "assignedTo": "userId"
      }
      
  • GET /api/tasks

    • Fetch all tasks. Includes optional filtering by priority, status, and assigned user.
    • Query parameters:
      • priority: Filter tasks by priority (e.g., low, medium, high).
      • status: Filter tasks by status (e.g., pending, in progress, completed).
      • assignedTo: Filter tasks by assigned user ID.
    • Response:
      [
        {
          "title": "Complete project report",
          "description": "Finish the report and submit it by the end of the week.",
          "priority": "high",
          "status": "pending",
          "assignedTo": "userId"
        }
      ]
      
  • GET /api/tasks/:id

    • Fetch a specific task by its ID.
    • Request parameters:
      • id: The ID of the task to retrieve.
    • Response:
      {
        "title": "Complete project report",
        "description": "Finish the report and submit it by the end of the week.",
        "priority": "high",
        "status": "pending",
        "assignedTo": "userId"
      }
      
  • PUT /api/tasks/:id

    • Update a specific task by its ID. Only accessible to users with the role admin.
    • Request body:
      {
        "title": "Updated task title",
        "description": "Updated task description",
        "priority": "medium",
        "status": "in progress",
        "assignedTo": "userId"
      }
      
    • Response:
      {
        "title": "Updated task title",
        "description": "Updated task description",
        "priority": "medium",
        "status": "in progress",
        "assignedTo": "userId"
      }
      
  • DELETE /api/tasks/:id

    • Delete a specific task by its ID. Only accessible to users with the role admin.
    • Request parameters:
      • id: The ID of the task to delete.
    • Response:
      {
        "message": "Task deleted"
      }
      

Roles and Permissions

  • User:

    • Can view tasks.
  • Admin:

    • Can perform all CRUD operations on tasks.

Sample Data

Register Admin User

  • Request Body:
    {
      "name": "Admin User",
      "email": "[email protected]",
      "password": "adminpassword",
      "role": "admin"
    }