using System.Text; using System.Text.Json.Serialization; using iknow_api.Data; using iknow_api.Repositories; using iknow_api.Services; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; var builder = WebApplication.CreateBuilder(args); // Add CORS policy builder.Services.AddCors(options => { options.AddPolicy("AllowFrontend", policy => { var allowedOrigins = builder.Configuration.GetSection("AllowedOrigins").Get() ?? new[] { "http://localhost:3000" }; policy.WithOrigins(allowedOrigins) // Your frontend URL(s) .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials(); }); }); builder.Services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options => { options.RequireHttpsMetadata = false; // only for development options.SaveToken = true; options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidIssuer = "iknow-api", ValidateAudience = true, ValidAudience = "iknow-api", ValidateLifetime = true, ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey( Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"])) }; }); // Add services to the container. builder.Services.AddControllers() .AddJsonOptions(options => { options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; options.JsonSerializerOptions.PropertyNameCaseInsensitive = true; options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); }); builder.Services.AddOpenApi(); // Register DbContext builder.Services.AddDbContext(options => options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))); // Register your services builder.Services.AddScoped(); builder.Services.AddScoped(); // You'll need to add the UserRepository implementation builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddAuthorization(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.MapOpenApi(); } // Enable CORS - must be called before UseAuthentication and UseAuthorization app.UseCors("AllowFrontend"); app.UseHttpsRedirection(); app.UseAuthentication(); // <-- must come BEFORE UseAuthorization app.UseAuthorization(); app.MapControllers(); app.Run();