source: iknow-api/Program.cs@ c0256f3

Last change on this file since c0256f3 was c0256f3, checked in by stefansaveski <stefansaveski19@…>, 11 months ago

Add user-related entities and enforce one-to-one mappings

Enhanced the User model with new entities: ContactInfo,
EnrollmentInfo, and HighSchool, establishing one-to-one
relationships. Updated UserDTO with additional fields and
introduced enums for type, quota, and major. Modified
UserRepository and AuthService to handle new entities.

Updated database schema with unique constraints on UserId
for related tables and added EF Core migration
20251027191028_dbRelations. Adjusted JSON serialization
to prevent circular references.

  • Property mode set to 100644
File size: 2.0 KB
Line 
1using System.Text;
2using System.Text.Json.Serialization;
3using iknow_api.Data;
4using iknow_api.Repositories;
5using iknow_api.Services;
6using Microsoft.AspNetCore.Authentication.JwtBearer;
7using Microsoft.EntityFrameworkCore;
8using Microsoft.IdentityModel.Tokens;
9
10var builder = WebApplication.CreateBuilder(args);
11builder.Services.AddAuthentication(options =>
12{
13 options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
14 options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
15})
16.AddJwtBearer(options =>
17{
18 options.RequireHttpsMetadata = false; // only for development
19 options.SaveToken = true;
20 options.TokenValidationParameters = new TokenValidationParameters
21 {
22 ValidateIssuer = true,
23 ValidIssuer = "iknow-api",
24
25 ValidateAudience = true,
26 ValidAudience = "iknow-api",
27
28 ValidateLifetime = true,
29
30 ValidateIssuerSigningKey = true,
31 IssuerSigningKey = new SymmetricSecurityKey(
32 Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
33 };
34});
35// Add services to the container.
36builder.Services.AddControllers()
37 .AddJsonOptions(options =>
38 {
39 options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
40 });
41builder.Services.AddOpenApi();
42
43// Register DbContext
44builder.Services.AddDbContext<AppDbContext>(options =>
45 options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
46
47// Register your services
48builder.Services.AddScoped<IAuthService, AuthService>();
49builder.Services.AddScoped<IUserRepository, UserRepository>(); // You'll need to add the UserRepository implementation
50builder.Services.AddScoped<IUserService, UserService>();
51
52builder.Services.AddAuthorization();
53var app = builder.Build();
54
55// Configure the HTTP request pipeline.
56if (app.Environment.IsDevelopment())
57{
58 app.MapOpenApi();
59}
60
61app.UseHttpsRedirection();
62app.UseAuthentication(); // <-- must come BEFORE UseAuthorization
63app.UseAuthorization();
64app.MapControllers();
65
66app.Run();
Note: See TracBrowser for help on using the repository browser.