2025-05-05 15:49:01 -03:00
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using GestionIntegral.Api.Data;
|
|
|
|
|
using GestionIntegral.Api.Services;
|
Fase 3:
- Backend API:
Autenticación y autorización básicas con JWT implementadas.
Cambio de contraseña funcional.
Módulo "Tipos de Pago" (CRUD completo) implementado en el backend (Controlador, Servicio, Repositorio) usando Dapper, transacciones y con lógica de historial.
Se incluyen permisos en el token JWT.
- Frontend React:
Estructura base con Vite, TypeScript, MUI.
Contexto de autenticación (AuthContext) que maneja el estado del usuario y el token.
Página de Login.
Modal de Cambio de Contraseña (forzado y opcional).
Hook usePermissions para verificar permisos.
Página GestionarTiposPagoPage con tabla, paginación, filtro, modal para crear/editar, y menú de acciones, respetando permisos.
Layout principal (MainLayout) con navegación por Tabs (funcionalidad básica de navegación).
Estructura de enrutamiento (AppRoutes) que maneja rutas públicas, protegidas y anidadas para módulos.
2025-05-07 13:41:18 -03:00
|
|
|
using GestionIntegral.Api.Services.Contables;
|
|
|
|
|
using GestionIntegral.Api.Data.Repositories;
|
2025-05-05 15:49:01 -03:00
|
|
|
|
2025-05-05 12:37:42 -03:00
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
2025-05-05 15:49:01 -03:00
|
|
|
// --- Registros de Servicios ---
|
|
|
|
|
builder.Services.AddSingleton<DbConnectionFactory>();
|
|
|
|
|
builder.Services.AddScoped<PasswordHasherService>();
|
Fase 3:
- Backend API:
Autenticación y autorización básicas con JWT implementadas.
Cambio de contraseña funcional.
Módulo "Tipos de Pago" (CRUD completo) implementado en el backend (Controlador, Servicio, Repositorio) usando Dapper, transacciones y con lógica de historial.
Se incluyen permisos en el token JWT.
- Frontend React:
Estructura base con Vite, TypeScript, MUI.
Contexto de autenticación (AuthContext) que maneja el estado del usuario y el token.
Página de Login.
Modal de Cambio de Contraseña (forzado y opcional).
Hook usePermissions para verificar permisos.
Página GestionarTiposPagoPage con tabla, paginación, filtro, modal para crear/editar, y menú de acciones, respetando permisos.
Layout principal (MainLayout) con navegación por Tabs (funcionalidad básica de navegación).
Estructura de enrutamiento (AppRoutes) que maneja rutas públicas, protegidas y anidadas para módulos.
2025-05-07 13:41:18 -03:00
|
|
|
builder.Services.AddScoped<IAuthRepository, AuthRepository>();
|
2025-05-05 15:49:01 -03:00
|
|
|
builder.Services.AddScoped<IAuthService, AuthService>();
|
Fase 3:
- Backend API:
Autenticación y autorización básicas con JWT implementadas.
Cambio de contraseña funcional.
Módulo "Tipos de Pago" (CRUD completo) implementado en el backend (Controlador, Servicio, Repositorio) usando Dapper, transacciones y con lógica de historial.
Se incluyen permisos en el token JWT.
- Frontend React:
Estructura base con Vite, TypeScript, MUI.
Contexto de autenticación (AuthContext) que maneja el estado del usuario y el token.
Página de Login.
Modal de Cambio de Contraseña (forzado y opcional).
Hook usePermissions para verificar permisos.
Página GestionarTiposPagoPage con tabla, paginación, filtro, modal para crear/editar, y menú de acciones, respetando permisos.
Layout principal (MainLayout) con navegación por Tabs (funcionalidad básica de navegación).
Estructura de enrutamiento (AppRoutes) que maneja rutas públicas, protegidas y anidadas para módulos.
2025-05-07 13:41:18 -03:00
|
|
|
builder.Services.AddScoped<ITipoPagoRepository, TipoPagoRepository>();
|
|
|
|
|
builder.Services.AddScoped<ITipoPagoService, TipoPagoService>();
|
2025-05-05 12:37:42 -03:00
|
|
|
|
2025-05-05 15:49:01 -03:00
|
|
|
// --- Configuración de Autenticación JWT ---
|
|
|
|
|
var jwtSettings = builder.Configuration.GetSection("Jwt");
|
|
|
|
|
var jwtKey = jwtSettings["Key"] ?? throw new ArgumentNullException("Jwt:Key", "JWT Key not configured in appsettings");
|
|
|
|
|
var keyBytes = Encoding.ASCII.GetBytes(jwtKey);
|
|
|
|
|
|
|
|
|
|
builder.Services.AddAuthentication(options =>
|
|
|
|
|
{
|
|
|
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
})
|
|
|
|
|
.AddJwtBearer(options =>
|
|
|
|
|
{
|
|
|
|
|
options.RequireHttpsMetadata = builder.Environment.IsProduction();
|
|
|
|
|
options.SaveToken = true;
|
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
|
{
|
|
|
|
|
ValidateIssuerSigningKey = true,
|
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(keyBytes),
|
|
|
|
|
ValidateIssuer = true,
|
|
|
|
|
ValidIssuer = jwtSettings["Issuer"],
|
|
|
|
|
ValidateAudience = true,
|
|
|
|
|
ValidAudience = jwtSettings["Audience"],
|
|
|
|
|
ValidateLifetime = true,
|
|
|
|
|
ClockSkew = TimeSpan.Zero
|
|
|
|
|
};
|
|
|
|
|
});
|
2025-05-05 12:37:42 -03:00
|
|
|
|
2025-05-05 15:49:01 -03:00
|
|
|
// --- Configuración de Autorización ---
|
|
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
|
|
|
|
|
|
// --- Configuración de CORS --- // <--- MOVER AQUÍ LA CONFIGURACIÓN DE SERVICIOS CORS
|
|
|
|
|
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
|
|
|
|
|
builder.Services.AddCors(options =>
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy(name: MyAllowSpecificOrigins,
|
|
|
|
|
policy =>
|
|
|
|
|
{
|
|
|
|
|
policy.WithOrigins("http://localhost:5173") // URL Frontend React
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowAnyMethod();
|
|
|
|
|
// Para pruebas más permisivas (NO USAR EN PRODUCCIÓN):
|
|
|
|
|
// policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
// --- Fin CORS ---
|
|
|
|
|
|
|
|
|
|
// --- Servicios del Contenedor ---
|
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
|
builder.Services.AddSwaggerGen();
|
2025-05-05 12:37:42 -03:00
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
2025-05-05 15:49:01 -03:00
|
|
|
// --- Configuración del Pipeline HTTP ---
|
2025-05-05 12:37:42 -03:00
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
{
|
2025-05-05 15:49:01 -03:00
|
|
|
app.UseSwagger();
|
|
|
|
|
app.UseSwaggerUI();
|
2025-05-05 12:37:42 -03:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 15:49:01 -03:00
|
|
|
// ¡¡¡NO USAR UseHttpsRedirection si tu API corre en HTTP!!!
|
|
|
|
|
// Comenta o elimina la siguiente línea si SÓLO usas http://localhost:5183
|
|
|
|
|
// app.UseHttpsRedirection(); // <--- COMENTAR/ELIMINAR SI NO USAS HTTPS EN API
|
|
|
|
|
|
|
|
|
|
// --- Aplicar CORS ANTES de Autenticación/Autorización ---
|
|
|
|
|
app.UseCors(MyAllowSpecificOrigins);
|
|
|
|
|
// --- Fin aplicar CORS ---
|
2025-05-05 12:37:42 -03:00
|
|
|
|
2025-05-05 15:49:01 -03:00
|
|
|
app.UseAuthentication();
|
2025-05-05 12:37:42 -03:00
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
2025-05-05 15:49:01 -03:00
|
|
|
app.Run();
|