TaskSync is a simple task management API built with Node.js, Express, TypeScript, and MongoDB. It supports user registration, login, and manager-only task creation, with JWT-based authentication and role-based access control.
- User registration and login (JWT authentication)
- Role-based access (Manager, Employee)
- Managers can create tasks for employees
- Input validation and error handling
git clone <your-repo-url>
cd TaskSyncnpm installCreate a .env file in the root directory (already present):
PORT=3000
JWT_SECRET="your_jwt_secret"
DB_URL=mongodb://root:rootpassword@localhost:27017/
If you don't have MongoDB locally, you can use Docker:
docker run -d \
--name tasksync-mongo \
-e MONGO_INITDB_ROOT_USERNAME=root \
-e MONGO_INITDB_ROOT_PASSWORD=rootpassword \
-p 27017:27017 \
mongo:latestnpm run devThe server will start on http://localhost:3000.
POST /api/v1/auth/register
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123",
"role": "Manager" // or "Employee"
}{
"data": {
"token": "<jwt_token>",
"user": {
"_id": "...",
"name": "John Doe",
"email": "john@example.com",
"role": "Manager",
"createdAt": "...",
"updatedAt": "..."
}
}
}- Validates input.
- Checks if email exists.
- Hashes password, creates user, generates JWT.
- Sets
tokencookie and returns user info (without password).
POST /api/v1/auth/login
{
"email": "john@example.com",
"password": "password123"
}{
"data": {
"token": "<jwt_token>",
"user": {
"_id": "...",
"name": "John Doe",
"email": "john@example.com",
"role": "Manager",
"createdAt": "...",
"updatedAt": "..."
}
}
}- Validates input.
- Checks if user exists and password matches.
- Generates JWT, sets
tokencookie, returns user info.
POST /api/v1/manager/
Authorization: Bearer <jwt_token>(or use thetokencookie)
{
"title": "Prepare Report",
"description": "Prepare the monthly sales report",
"assignedTo": "<employee_user_id>",
"dueDate": "2024-07-01T00:00:00.000Z"
}{
"data": {
"_id": "...",
"title": "Prepare Report",
"description": "Prepare the monthly sales report",
"assignedTo": "<employee_user_id>",
"dueDate": "2024-07-01T00:00:00.000Z",
"createdBy": "<manager_user_id>",
"createdAt": "...",
"updatedAt": "...",
"status": "Pending"
}
}- Requires authentication (
authMiddleware). - Requires manager role (
managerOnlymiddleware). - Validates input.
- Creates a new task assigned to an employee, with the manager as
createdBy. - Returns the created task.
GET /api/v1/auth/employees
Authorization: Bearer <jwt_token>(must be a manager)
{
"data": [
{
"_id": "...",
"name": "Jane Employee",
"email": "jane@company.com"
}
// ...more employees
]
}- Requires authentication (
authMiddleware). - Requires manager role (
managerOnlymiddleware). - Returns a list of all users with the role "Employee" (name and email only).
GET /api/v1/auth/:id
Authorization: Bearer <jwt_token>
{
"data": {
"_id": "...",
"name": "John Doe",
"email": "john@example.com"
}
}- Requires authentication (
authMiddleware). - Returns the user with the specified ID (name and email only).
- Returns 404 if user is not found.
GET /api/v1/employee/my-tasks
Authorization: Bearer <jwt_token>(must be an employee)
{
"data": [
{
"_id": "...",
"title": "Prepare Report",
"description": "Prepare the monthly sales report",
"assignedTo": "<employee_user_id>",
"dueDate": "2024-07-01T00:00:00.000Z",
"createdBy": "<manager_user_id>",
"createdAt": "...",
"updatedAt": "...",
"status": "Pending"
}
// ...more tasks
]
}- Requires authentication (
authMiddleware). - Returns all tasks assigned to the authenticated employee, sorted by due date.
PATCH /api/v1/employee/:taskId/status
Authorization: Bearer <jwt_token>(must be an employee)
{
"status": "Completed" // or "Pending" or "In Progress"
}{
"data": {
"_id": "...",
"title": "Prepare Report",
"description": "Prepare the monthly sales report",
"assignedTo": "<employee_user_id>",
"dueDate": "2024-07-01T00:00:00.000Z",
"createdBy": "<manager_user_id>",
"createdAt": "...",
"updatedAt": "...",
"status": "Completed"
}
}- Requires authentication (
authMiddleware). - Validates
taskIdandstatusin the request. - Checks that the task is assigned to the authenticated employee.
- Updates the status and
updatedAtof the task. - Returns the updated task.
GET /api/v1/manager/created
Authorization: Bearer <jwt_token>(must be a manager)
{
"data": [
{
"_id": "...",
"title": "Prepare Report",
"description": "Prepare the monthly sales report",
"assignedTo": "<employee_user_id>",
"dueDate": "2024-07-01T00:00:00.000Z",
"createdBy": "<manager_user_id>",
"createdAt": "...",
"updatedAt": "...",
"status": "Pending"
}
// ...more tasks
]
}- Requires authentication (
authMiddleware). - Requires manager role (
managerOnlymiddleware). - Returns all tasks created by the authenticated manager.
PUT /api/v1/manager/:taskId
Authorization: Bearer <jwt_token>(must be a manager)
{
"title": "Updated Title",
"description": "Updated description",
"status": "In Progress",
"dueDate": "2024-08-01T00:00:00.000Z",
"assignedTo": "<employee_user_id>"
}{
"data": {
"_id": "...",
"title": "Updated Title",
"description": "Updated description",
"assignedTo": "<employee_user_id>",
"dueDate": "2024-08-01T00:00:00.000Z",
"createdBy": "<manager_user_id>",
"createdAt": "...",
"updatedAt": "...",
"status": "In Progress"
}
}- Requires authentication (
authMiddleware). - Requires manager role (
managerOnlymiddleware). - Validates
taskIdand input fields. - Checks that the task was created by the authenticated manager.
- Updates the task and returns the updated task.
DELETE /api/v1/manager/:taskId
Authorization: Bearer <jwt_token>(must be a manager)
{
"data": {
"_id": "...",
"title": "..."
// ...other task fields
}
}- Requires authentication (
authMiddleware). - Requires manager role (
managerOnlymiddleware). - Validates
taskId. - Checks that the task was created by the authenticated manager.
- Deletes the task and returns the deleted task.
All errors return a JSON response:
{
"error": {
"message": "Error description"
}
}- Use the
/api/v1/auth/registerendpoint to create both managers and employees. - Only managers (users with role
"Manager") can create tasks. - JWT token is returned and also set as a cookie for authentication.
See the repository for a detailed folder and file structure.
You can also use a docker-compose.yml for MongoDB:
version: "3"
services:
mongo:
image: mongo:latest
container_name: tasksync-mongo
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: rootpassword
ports:
- "27017:27017"Start with:
docker-compose up -dPull requests are welcome!
MIT