source: iknow-api/Program.cs@ cab02ad

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

Add user-related models, services, and JWT validation

Added ContactInfo, EnrollmentInfo, and HighSchool models with relationships to the User entity. Updated AppDbContext and migrations to reflect these changes. Introduced UserService and IUserService for handling user data retrieval via JWT validation. Added UserController with a getUser endpoint to fetch user data based on JWT.

Enhanced IUserRepository and UserRepository with a method to fetch users by ID. Registered UserService in the DI container. Defined enums for EnrollmentInfo and HighSchool. Removed outdated migration files and performed general refactoring and cleanup.

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