This backend is part of the trash pickup system for Brasília. It uses:
- Node.js and Express.js for the backend server.
- MongoDB Atlas as the database.
- JWT-based authentication for secure user access.
I have set up the MongoDB Atlas database for the project. The details are:
- Login Email: egalitarianbrasil@proton.me
- username: egalitarianbrasil
- The Free instance is hosted on GCP in Sao Paulo, Brasil.
- Password: Remi has the password and can share it with the team.
- Access: Currently, the database allows access from any IP location for easier development.
You can view the database cluster and data at MongoDB Atlas. The database name is: egalitarian_db.
Follow these steps to set up the backend on your local machine:
git clone <repository_url>
cd <project_folder>Run the following command to install required Node.js packages:
npm installCreate a .env file in the root of the project. Use the .env.example as a template:
cp .env.example .envEdit the .env file with your credentials:
MONGO_URI=mongodb+srv://ecolink-devs:QgwYjRVfy11LXX5q@brasil.sk6i5.mongodb.net/?retryWrites=true&w=majority&appName=brasil
JWT_SECRET=<your random secret jwt>
PORT=5000
To get the correct connection string contact backend developers (Not shared publicly in repository).
Start the server locally:
npx nodemon server.jsThe backend will run on http://192.168.0.168:5000.
We implemented user registration and login functionality with JWT authentication.
-
POST
/api/auth/register: Register a new user (admin role). Payload:{ "username": "Admin", "email": "admin@example.com", "password": "password123", "phone": "9283372629", "cpf": "12466748982", "roleId": "6835fe9db57507b46d1e7369" }Expected Response:
{ "message": "User registered successfully" } -
POST
/api/auth/login: Authenticate an existing user. Payload:{ "email": "admin@example.com", "password": "password123" }Expected Response:
{ "token": "<jwt_token>", "user": {} }
Administrator(Admin) can change the role of other users.
If you want to create a Admin or Editor user you need define the role id, if not the user will be viewer by default.
Admin id: 6835fe9db57507b46d1e7369
Editor id: 6836082d82cf7e288f7ca46d
Viewer id (default): 683607d382cf7e288f7ca460
-
GET
/api/roles: Get all the roles informations. Payload: noneExpected Response:
{ { "_id": "", "name": "", "description": "", "createdAt": "", "__v": 0 } }
-
GET
/api/users/me: Get logged in user. Payload: noneExpected Response:
{ { "id": "", "username": "", "email": "", } } -
GET
/api/users: Get all the users (only for admin). Payload: noneExpected Response: Array with all the users
{ { "_id": "", "username": "", "email": "", "roleId": {} } }
We implemented middleware to secure protected routes using JWT authentication.
- Middleware:
authMiddleware.jsverifies tokens from the Authorization header. - Example Protected Route: GET
/api/users/meRequires Authorization: Bearer<jwt_token>header. Expected Response:{ "id": "user_id_here", "username": "testuser", "email": "test@example.com" }
Returns all uploaded media.
Optional query parameter ?category=example_category can be used to filter by category.
Headers:
Authorization: Bearer <jwt_token>
Expected Response (example):
[
{
"_id": "abc123",
"filename": "example.jpg",
"path": "uploads\\example.jpg",
"type": "image/jpg",
"category": "Visit",
"uploadedAt": "2025-05-30T14:12:00.000Z"
}
]Uploads a new media file.
Headers:
Authorization: Bearer <jwt_token>
Content-Type: multipart/form-data
Payload (FormData):
file: <file>
category: Education
Expected Response:
{
"message": "File uploaded successfully"
}Returns a list of unique categories from the media files.
Headers:
Authorization: Bearer <jwt_token>
Expected Response:
["Visit", "Collect", "Storage"]I tested the local endpoints with Postman:
- Successfully registered and logged in a test user.
- Verified the JWT token-based authentication for protected routes.
- Confirmed MongoDB Atlas stores the test user in the
userscollection.
To run tests:
npx jest
There are two tests for "roles", both test if the roles exists and if they are correct.
- must return all predefined roles
- must return exactly 3 roles
Continue developing the backend, including additional routes like waste pickup APIs.