| [e49c3ed] | 1 | using System.Text;
|
|---|
| [b38c39c] | 2 | using System.Text.Json.Serialization;
|
|---|
| [e49c3ed] | 3 | using iknow_api.Data;
|
|---|
| [277bd72] | 4 | using iknow_api.Repositories;
|
|---|
| [e49c3ed] | 5 | using iknow_api.Services;
|
|---|
| 6 | using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|---|
| 7 | using Microsoft.EntityFrameworkCore;
|
|---|
| 8 | using Microsoft.IdentityModel.Tokens;
|
|---|
| [2859225] | 9 |
|
|---|
| [0b16523] | 10 | var builder = WebApplication.CreateBuilder(args);
|
|---|
| [bb02e3a] | 11 |
|
|---|
| 12 | // Add CORS policy
|
|---|
| 13 | builder.Services.AddCors(options =>
|
|---|
| 14 | {
|
|---|
| 15 | options.AddPolicy("AllowFrontend", policy =>
|
|---|
| 16 | {
|
|---|
| 17 | var allowedOrigins = builder.Configuration.GetSection("AllowedOrigins").Get<string[]>()
|
|---|
| 18 | ?? new[] { "http://localhost:3000" };
|
|---|
| 19 |
|
|---|
| 20 | policy.WithOrigins(allowedOrigins) // Your frontend URL(s)
|
|---|
| 21 | .AllowAnyHeader()
|
|---|
| 22 | .AllowAnyMethod()
|
|---|
| 23 | .AllowCredentials();
|
|---|
| 24 | });
|
|---|
| 25 | });
|
|---|
| 26 |
|
|---|
| [e49c3ed] | 27 | builder.Services.AddAuthentication(options =>
|
|---|
| 28 | {
|
|---|
| 29 | options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|---|
| 30 | options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|---|
| 31 | })
|
|---|
| 32 | .AddJwtBearer(options =>
|
|---|
| 33 | {
|
|---|
| 34 | options.RequireHttpsMetadata = false; // only for development
|
|---|
| 35 | options.SaveToken = true;
|
|---|
| 36 | options.TokenValidationParameters = new TokenValidationParameters
|
|---|
| 37 | {
|
|---|
| 38 | ValidateIssuer = true,
|
|---|
| 39 | ValidIssuer = "iknow-api",
|
|---|
| 40 |
|
|---|
| 41 | ValidateAudience = true,
|
|---|
| 42 | ValidAudience = "iknow-api",
|
|---|
| 43 |
|
|---|
| 44 | ValidateLifetime = true,
|
|---|
| 45 |
|
|---|
| 46 | ValidateIssuerSigningKey = true,
|
|---|
| 47 | IssuerSigningKey = new SymmetricSecurityKey(
|
|---|
| 48 | Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
|
|---|
| 49 | };
|
|---|
| 50 | });
|
|---|
| [0b16523] | 51 | // Add services to the container.
|
|---|
| [b38c39c] | 52 | builder.Services.AddControllers()
|
|---|
| 53 | .AddJsonOptions(options =>
|
|---|
| 54 | {
|
|---|
| 55 | options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
|
|---|
| [c2f79d7] | 56 | options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
|
|---|
| 57 | options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
|---|
| [b38c39c] | 58 | });
|
|---|
| [0b16523] | 59 | builder.Services.AddOpenApi();
|
|---|
| 60 |
|
|---|
| [5b42c8d] | 61 | // Register DbContext
|
|---|
| [5aa102d] | 62 | builder.Services.AddDbContext<AppDbContext>(options =>
|
|---|
| 63 | options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
|
|---|
| 64 |
|
|---|
| [5b42c8d] | 65 | // Register your services
|
|---|
| 66 | builder.Services.AddScoped<IAuthService, AuthService>();
|
|---|
| 67 | builder.Services.AddScoped<IUserRepository, UserRepository>(); // You'll need to add the UserRepository implementation
|
|---|
| [cab02ad] | 68 | builder.Services.AddScoped<IUserService, UserService>();
|
|---|
| 69 |
|
|---|
| [3347c1b] | 70 | builder.Services.AddScoped<IRefreshTokenService, RefreshTokenService>();
|
|---|
| 71 | builder.Services.AddScoped<IUserRepository, UserRepository>();
|
|---|
| 72 | builder.Services.AddScoped<IRefreshTokenRepository, RefreshTokenRepository>();
|
|---|
| [e49c3ed] | 73 | builder.Services.AddAuthorization();
|
|---|
| [0b16523] | 74 | var app = builder.Build();
|
|---|
| 75 |
|
|---|
| 76 | // Configure the HTTP request pipeline.
|
|---|
| 77 | if (app.Environment.IsDevelopment())
|
|---|
| 78 | {
|
|---|
| 79 | app.MapOpenApi();
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| [bb02e3a] | 82 | // Enable CORS - must be called before UseAuthentication and UseAuthorization
|
|---|
| 83 | app.UseCors("AllowFrontend");
|
|---|
| 84 |
|
|---|
| [0b16523] | 85 | app.UseHttpsRedirection();
|
|---|
| [e49c3ed] | 86 | app.UseAuthentication(); // <-- must come BEFORE UseAuthorization
|
|---|
| [0b16523] | 87 | app.UseAuthorization();
|
|---|
| 88 | app.MapControllers();
|
|---|
| 89 |
|
|---|
| 90 | app.Run();
|
|---|