This is the backend service for a coding contest team management system. It allows users to register, log in, manage CRUD operations, create teams, add members, and manage a leaderboard.
- User authentication using JWT (JSON Web Tokens).
- Role-based access control (Team Leader, Super Admin).
- CRUD operations for users and teams.
- Leaderboard management by Super Admin.
- Node.js: Runtime environment.
- Express: Web framework.
- MongoDB: Database for storing data.
- JWT: Token-based authentication.
- dotenv: For managing environment variables.
-
Clone the repository:
git clone <repository_url> cd team-management-backend
-
Install dependencies:
npm install
-
Set up the
.env
file with the following variables:PORT=5000 MONGO_URI=<your_mongodb_connection_string> JWT_SECRET=<your_secret_key>
-
Start the server:
npm start
The server will run at
http://localhost:5000
.
- Endpoint:
POST /api/users/register
- Description: Register a new user.
- Request Body:
{ "name": "John Doe", "email": "[email protected]", "password": "password123", "role": "Team Leader" // or "Member" }
- Response:
{ "message": "User registered successfully", "user": { "id": "user_id", "name": "John Doe", "email": "[email protected]", "role": "Team Leader" } }
- Endpoint:
POST /api/users/login
- Description: Authenticate and log in a user.
- Request Body:
{ "email": "[email protected]", "password": "password123" }
- Response:
{ "token": "jwt_token", "user": { "id": "user_id", "name": "John Doe", "role": "Team Leader" } }
- Endpoint:
POST /api/teams
- Description: Create a new team (Only accessible to Team Leaders).
- Headers:
{ "Authorization": "Bearer <jwt_token>" }
- Request Body:
{ "name": "Team Alpha" }
- Response:
{ "message": "Team created" }
- Endpoint:
PUT /api/teams/:id/members
- Description: Add a member to a team (Only accessible to Team Leaders).
- Headers:
{ "Authorization": "Bearer <jwt_token>" }
- Request Body:
{ "memberId": "user_id" }
- Response:
{ "message": "Member added" }
- Endpoint:
PUT /api/leaderboard
- Description: Update the leaderboard rankings (Only accessible to Super Admins).
- Headers:
{ "Authorization": "Bearer <jwt_token>" }
- Request Body:
{ "teamId": "team_id", "rank": 1 }
- Response:
{ "message": "Leaderboard updated" }
- File:
middleware/auth.js
- Validates JWT tokens and ensures the user is authenticated.
- File:
middleware/role.js
- Ensures users have the correct role (e.g.,
Team Leader
orSuper Admin
) for specific routes.
team-management-backend/
├── models/
│ ├── User.js # User model schema
│ ├── Team.js # Team model schema
├── routes/
│ ├── userRoutes.js # Routes for user authentication
│ ├── teamRoutes.js # Routes for team management
├── middleware/
│ ├── auth.js # Authentication middleware
│ ├── role.js # Role-based middleware
├── .env # Environment variables
├── server.js # Main entry point
├── package.json # Dependencies and scripts
-
Register and Login:
- Use the
POST /api/users/register
andPOST /api/users/login
endpoints to create a user and log in. - Copy the
jwt_token
from the login response.
- Use the
-
Create a Team:
- Use the
POST /api/teams
endpoint with the token in the Authorization header.
- Use the
-
Add Team Member:
- Use the
PUT /api/teams/:id/members
endpoint with the token and the member's ID.
- Use the
-
Update Leaderboard:
- Use the
PUT /api/leaderboard
endpoint as a Super Admin.
- Use the
-
Frontend Integration:
- Make API calls from your frontend using tools like Axios or Fetch.
- Include the JWT token in the
Authorization
header for authenticated requests.
-
Environment Variables:
- Use
.env
to configure the API base URL and secret keys securely.
- Use
-
Deployment:
- Host the backend on platforms like Heroku, AWS, or Vercel.
- Use services like MongoDB Atlas for database hosting.
For any issues or queries, please contact the developer.