source: iknow-api/Program.cs@ bf18ca4

Last change on this file since bf18ca4 was bf18ca4, checked in by Stefan-Saveski <stefansaveski19@…>, 9 months ago

Added dockerfile and changes to GET user.

  • Property mode set to 100644
File size: 2.9 KB
RevLine 
[5eb7847]1using System.Text;
[c0256f3]2using System.Text.Json.Serialization;
[5eb7847]3using iknow_api.Data;
[271178d]4using iknow_api.Repositories;
[5eb7847]5using iknow_api.Services;
6using Microsoft.AspNetCore.Authentication.JwtBearer;
7using Microsoft.EntityFrameworkCore;
8using Microsoft.IdentityModel.Tokens;
[618be76]9
[c11d3a3]10var builder = WebApplication.CreateBuilder(args);
[bf18ca4]11
12// Add CORS policy
13builder.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
[5eb7847]27builder.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});
[c11d3a3]51// Add services to the container.
[c0256f3]52builder.Services.AddControllers()
53 .AddJsonOptions(options =>
54 {
55 options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
[f0fc386]56 options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
57 options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
[c0256f3]58 });
[c11d3a3]59builder.Services.AddOpenApi();
60
[fc7b4b4]61// Register DbContext
[c0a75ab]62builder.Services.AddDbContext<AppDbContext>(options =>
63 options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
64
[fc7b4b4]65// Register your services
66builder.Services.AddScoped<IAuthService, AuthService>();
67builder.Services.AddScoped<IUserRepository, UserRepository>(); // You'll need to add the UserRepository implementation
[4f5d8fd]68builder.Services.AddScoped<IUserService, UserService>();
69
[4e061d8]70builder.Services.AddScoped<IRefreshTokenService, RefreshTokenService>();
71builder.Services.AddScoped<IUserRepository, UserRepository>();
72builder.Services.AddScoped<IRefreshTokenRepository, RefreshTokenRepository>();
[5eb7847]73builder.Services.AddAuthorization();
[c11d3a3]74var app = builder.Build();
75
76// Configure the HTTP request pipeline.
77if (app.Environment.IsDevelopment())
78{
79 app.MapOpenApi();
80}
81
[bf18ca4]82// Enable CORS - must be called before UseAuthentication and UseAuthorization
83app.UseCors("AllowFrontend");
84
[c11d3a3]85app.UseHttpsRedirection();
[5eb7847]86app.UseAuthentication(); // <-- must come BEFORE UseAuthorization
[c11d3a3]87app.UseAuthorization();
88app.MapControllers();
89
90app.Run();
Note: See TracBrowser for help on using the repository browser.