Changes in / [af4f801:c72ed3b]


Ignore:
Files:
35 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • iknow-api/Program.cs

    raf4f801 rc72ed3b  
    1 using System.Text;
    2 using System.Text.Json.Serialization;
    3 using iknow_api.Data;
    4 using iknow_api.Repositories;
    5 using iknow_api.Services;
    6 using Microsoft.AspNetCore.Authentication.JwtBearer;
    7 using Microsoft.EntityFrameworkCore;
    8 using Microsoft.IdentityModel.Tokens;
    9 
    101var builder = WebApplication.CreateBuilder(args);
    112
    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 });
     3// Add services to the container.
    264
    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 });
    51 // Add services to the container.
    52 builder.Services.AddControllers()
    53     .AddJsonOptions(options =>
    54     {
    55         options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
    56         options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
    57         options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
    58     });
     5builder.Services.AddControllers();
     6// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
    597builder.Services.AddOpenApi();
    608
    61 // Register DbContext
    62 builder.Services.AddDbContext<AppDbContext>(options =>
    63     options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
    64 
    65 // Register your services
    66 builder.Services.AddScoped<IAuthService, AuthService>();
    67 builder.Services.AddScoped<IUserRepository, UserRepository>(); // You'll need to add the UserRepository implementation
    68 builder.Services.AddScoped<IUserService, UserService>();
    69 
    70 builder.Services.AddScoped<IRefreshTokenService, RefreshTokenService>();
    71 builder.Services.AddScoped<IUserRepository, UserRepository>();
    72 builder.Services.AddScoped<IRefreshTokenRepository, RefreshTokenRepository>();
    73 builder.Services.AddAuthorization();
    749var app = builder.Build();
    7510
    … …  
    8015}
    8116
    82 // Enable CORS - must be called before UseAuthentication and UseAuthorization
    83 app.UseCors("AllowFrontend");
     17app.UseHttpsRedirection();
    8418
    85 app.UseHttpsRedirection();
    86 app.UseAuthentication(); // <-- must come BEFORE UseAuthorization
    8719app.UseAuthorization();
     20
    8821app.MapControllers();
    8922
  • iknow-api/appsettings.Development.json

    raf4f801 rc72ed3b  
    55      "Microsoft.AspNetCore": "Warning"
    66    }
    7   },
    8   "ConnectionStrings": {
    9     "DefaultConnection": "Host=ep-divine-term-aghfotgc-pooler.c-2.eu-central-1.aws.neon.tech; Database=neondb; Username=neondb_owner; Password=npg_SxbBZmtw4I7P; SSL Mode=VerifyFull; Channel Binding=Require;"
    107  }
    118}
  • iknow-api/appsettings.json

    raf4f801 rc72ed3b  
    66    }
    77  },
    8   "AllowedOrigins": [
    9     "http://localhost:3000",
    10     "https://iknow-remaster.vercel.app"
    11   ],
    12   "ConnectionStrings": {
    13     "DefaultConnection": "Host=ep-divine-term-aghfotgc-pooler.c-2.eu-central-1.aws.neon.tech; Database=neondb; Username=neondb_owner; Password=npg_SxbBZmtw4I7P; SSL Mode=VerifyFull; Channel Binding=Require;"
    14   },
    15   "Jwt": {
    16     "Key": "YourSecretKeyHereMustBeAtLeast32CharactersLongForHS256Algorithm"
    17   }
     8  "AllowedHosts": "*"
    189}
Note: See TracChangeset for help on using the changeset viewer.