| [5eb7847] | 1 | using System.Text;
|
|---|
| [c0256f3] | 2 | using System.Text.Json.Serialization;
|
|---|
| [5eb7847] | 3 | using iknow_api.Data;
|
|---|
| [271178d] | 4 | using iknow_api.Repositories;
|
|---|
| [5eb7847] | 5 | using iknow_api.Services;
|
|---|
| 6 | using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|---|
| 7 | using Microsoft.EntityFrameworkCore;
|
|---|
| 8 | using Microsoft.IdentityModel.Tokens;
|
|---|
| [618be76] | 9 |
|
|---|
| [c11d3a3] | 10 | var builder = WebApplication.CreateBuilder(args);
|
|---|
| [5eb7847] | 11 | builder.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 | });
|
|---|
| [c11d3a3] | 35 | // Add services to the container.
|
|---|
| [c0256f3] | 36 | builder.Services.AddControllers()
|
|---|
| 37 | .AddJsonOptions(options =>
|
|---|
| 38 | {
|
|---|
| 39 | options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
|
|---|
| 40 | });
|
|---|
| [c11d3a3] | 41 | builder.Services.AddOpenApi();
|
|---|
| 42 |
|
|---|
| [fc7b4b4] | 43 | // Register DbContext
|
|---|
| [c0a75ab] | 44 | builder.Services.AddDbContext<AppDbContext>(options =>
|
|---|
| 45 | options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
|
|---|
| 46 |
|
|---|
| [fc7b4b4] | 47 | // Register your services
|
|---|
| 48 | builder.Services.AddScoped<IAuthService, AuthService>();
|
|---|
| 49 | builder.Services.AddScoped<IUserRepository, UserRepository>(); // You'll need to add the UserRepository implementation
|
|---|
| [4f5d8fd] | 50 | builder.Services.AddScoped<IUserService, UserService>();
|
|---|
| 51 |
|
|---|
| [4e061d8] | 52 | builder.Services.AddScoped<IRefreshTokenService, RefreshTokenService>();
|
|---|
| 53 | builder.Services.AddScoped<IUserRepository, UserRepository>();
|
|---|
| 54 | builder.Services.AddScoped<IRefreshTokenRepository, RefreshTokenRepository>();
|
|---|
| [5eb7847] | 55 | builder.Services.AddAuthorization();
|
|---|
| [c11d3a3] | 56 | var app = builder.Build();
|
|---|
| 57 |
|
|---|
| 58 | // Configure the HTTP request pipeline.
|
|---|
| 59 | if (app.Environment.IsDevelopment())
|
|---|
| 60 | {
|
|---|
| 61 | app.MapOpenApi();
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | app.UseHttpsRedirection();
|
|---|
| [5eb7847] | 65 | app.UseAuthentication(); // <-- must come BEFORE UseAuthorization
|
|---|
| [c11d3a3] | 66 | app.UseAuthorization();
|
|---|
| 67 | app.MapControllers();
|
|---|
| 68 |
|
|---|
| 69 | app.Run();
|
|---|