User Module Documentation - EML Backend
1. Introduction
The Users module in EML Backend manages the creation, retrieval, and administration of users within the platform. It enables role management, permissions, and user profiles.
2. Code Structure
modules/users/
├── users.module.ts # Module definition
├── users.controller.ts # Controller for handling HTTP requests
├── users.service.ts # Business logic
├── users.repository.ts # Database access
├── dto/ # Data Transfer Object (DTO) definitions
│ ├── create-user.dto.ts
│ ├── update-user.dto.ts
├── entities/ # Database model definitions
│ ├── user.entity.ts
3. Available Endpoints
3.1 Get User List
GET /users - Returns a list of users.
Request Example:
curl -X GET http://localhost:3000/users -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5c..."
Response Example:
[
{
"id": 1,
"name": "Juan Perez",
"email": "[email protected]",
"role": "admin"
},
{
"id": 2,
"name": "María Gómez",
"email": "[email protected]",
"role": "user"
}
]
3.2 Get User by ID
GET /users/:id - Retrieves a user by ID.
Request Example:
curl -X GET http://localhost:3000/users/1 -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5c..."
Response Example:
{
"id": 1,
"name": "Juan Perez",
"email": "[email protected]",
"role": "admin"
}
3.3 Create User
POST /users - Creates a new user.
Request Example:
curl -X POST http://localhost:3000/users -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5c..." -d '{"name": "Carlos Ruiz", "email": "[email protected]", "password": "123456", "role": "user"}'
Response Example:
{
"id": 3,
"name": "Carlos Ruiz",
"email": "[email protected]",
"role": "user"
}
3.4 Update User
PUT /users/:id - Modifies user data.
Request Example:
curl -X PUT http://localhost:3000/users/1 -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5c..." -d '{"name": "Juan P. Pérez", "role": "superadmin"}'
Response Example:
{
"id": 1,
"name": "Juan P. Pérez",
"email": "[email protected]",
"role": "superadmin"
}
3.5 Delete User
DELETE /users/:id - Deletes a user by ID.
Request Example:
curl -X DELETE http://localhost:3000/users/1 -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5c..."
Response Example:
{
"message": "User successfully deleted."
}
4. Security and Middleware
- AuthGuard: Protects endpoints with JWT authentication.
- RolesGuard: Middleware that validates permissions based on user roles.
Example of protection using RolesGuard:
@Get()
@UseGuards(AuthGuard('jwt'), RolesGuard)
@Roles('admin')
async getUsers() {
return this.userService.findAll();
}
5. Dependencies with Other Modules
- Authentication: Requires the authentication module to validate credentials.
- Notifications: Can send welcome emails or role change alerts.
6. Additional Notes
- It is recommended to enable password recovery and email verification in future versions.
- Pagination will be implemented in the user list to improve performance.
7. Conclusion
The Users module in EML Backend is essential for managing profiles and access control within the platform. It allows secure and scalable user account management.
🚀 This document will be updated as the system evolves.