Users API
Relevant source files
The following files were used as context for generating this wiki page:
- Back/Alumnos/Api/Controllers/UsuariosController.cs
- Back/Alumnos/Api/Entidades/Usuario.cs
- Back/Alumnos/Api/Comun/Modelos/Usuarios/CrearUsuarioDto.cs
- Back/Alumnos/Api/Comun/Modelos/Usuarios/ModificarUsuarioDto.cs
- Back/Alumnos/Api/Comun/Utilidades/MapeoDtos.cs
- Front/src/services/api.ts
- Back/Alumnos/Api/Servicios/UsuarioSesionServicio.cs
The Users API is a core module of the system responsible for managing user identities, profiles, and authentication states. It provides a RESTful interface for the frontend to perform CRUD operations on user accounts, handle registration, and manage session states through secure hashing and token-based authentication.
The system is split between a C# .NET Backend that handles persistence and business logic, and a React/TypeScript Frontend that consumes these services through a centralized API layer.
Backend Architecture
The backend follows a Controller-Service-Repository pattern. The UsuariosController acts as the primary entry point, utilizing dependency injection for database context and security services.
Core Components
- Controller: Manages HTTP requests for fetching, creating, and updating users.
- DTOs (Data Transfer Objects): Used to decouple the internal database entities from the external API surface.
- Entities: Represents the database schema for the
Usuariotable. - Security Services: Handles password hashing (
IHasherServicio) and session management.
API Endpoints
The API is exposed under the /usuarios route. Most operations utilize a slug (unique string identifier) instead of numeric IDs for external references.
| Method | Endpoint | Description | Payload |
|---|---|---|---|
| GET | /usuarios | List users filtered by name/status | Query Params: nombre, habilitado |
| GET | /usuarios/{slug} | Get specific user details | N/A |
| POST | /usuarios | Register a new user account | CrearUsuarioDto |
| PUT | /usuarios/{slug} | Update existing user profile | ModificarUsuarioDto |
| PATCH | /usuarios/{slug} | Enable/Disable a user account | HabilitadoUsuarioDto |
Sources: Back/Alumnos/Api/Controllers/UsuariosController.cs:27-109, Front/src/services/api.js:24-74
Data Flow: User Registration
When a user registers, the password is encrypted using a hashing service before being persisted to the SQL database. The system then generates and returns a unique slug.
Frontend Integration
The frontend manages user interactions through a service layer in api.ts. It utilizes axios for HTTP requests and maintains a bearer token in localStorage for authenticated requests.
User Service Methods
obtenerUsuario(slug): Fetches profile data for theProfilepage.modificarUsuario(slug, datos): Sends updated profile information.actualizarImagen(slug, imagen): Handles profile picture uploads viaFormData.
// Example of the Update User Profile service
export const usuariosService = {
modificarUsuario: async (slug: string, datos: Partial<Usuario>): Promise<Usuario> => {
try {
const response = await api.put(`/usuarios/${slug}`, datos);
return response.data;
} catch (error) {
throw new Error('Error al modificar usuario: ' + error.message);
}
}
};
Sources: Front/src/services/api.ts:79-86, Front/src/pages/profile.jsx:87-105
Security and Sessions
User sessions are managed via the UsuarioSesionServicio. Upon login, the system validates the NombreUsuario and Contraseña, creating a SesionUsuario record that tracks the start date and validity.
Authentication Attributes
- Hashing: Utilizes
Sha512HasherServiciofor password security. - Persistence: Sessions can be flagged as persistent via
MantenerSesion. - Authorization: The controller contains placeholders for
[Authorize]attributes to protect sensitive endpoints.
Sources: Back/Alumnos/Api/Servicios/UsuarioSesionServicio.cs:22-48, Back/Alumnos/Api/Program.cs:23-24
Summary
The Users API provides a robust foundation for account management within the application. By utilizing DTOs for data transfer and a slug-based identification system, it ensures a clear separation between database implementation and the public interface, while the integration of hashing services and session tracking provides the necessary security for user data.