diff --git a/Infrastructure/ApplicationDbContext.cs b/Infrastructure/ApplicationDbContext.cs new file mode 100644 index 0000000..21a41ec --- /dev/null +++ b/Infrastructure/ApplicationDbContext.cs @@ -0,0 +1,13 @@ + +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; + +namespace Infrastructure; + +public class ApplicationDbContext : IdentityDbContext +{ + public ApplicationDbContext(DbContextOptions options) : + base(options) + { } +} \ No newline at end of file diff --git a/Infrastructure/Infrastructure.csproj b/Infrastructure/Infrastructure.csproj index d938b2e..0407efb 100644 --- a/Infrastructure/Infrastructure.csproj +++ b/Infrastructure/Infrastructure.csproj @@ -8,6 +8,8 @@ + + all diff --git a/Server/FirstTest.db b/Server/FirstTest.db new file mode 100644 index 0000000..2fa9246 Binary files /dev/null and b/Server/FirstTest.db differ diff --git a/Server/Program.cs b/Server/Program.cs index 1dee5c0..a5e811f 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -6,6 +6,8 @@ using Server.Services; using Microsoft.OpenApi.Models; using Infrastructure.Repositories; using AutoMapper; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; using Server.Mappers; var builder = WebApplication.CreateBuilder(args); @@ -34,6 +36,8 @@ builder.Services.AddAutoMapper(typeof(PaginatedResultProfile)); builder.Services.AddAutoMapper(typeof(TrainingProgramProfile)); + + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); @@ -50,9 +54,41 @@ builder.Services.AddApiVersioning(options => options.SubstituteApiVersionInUrl = true; }); -builder.Services.AddSwaggerGen(c => - c.SwaggerDoc("v1", new OpenApiInfo { Title = "Optifit API", Version = "v1" }) -); +builder.Services.AddSwaggerGen(option => +{ + option.SwaggerDoc("v1", new OpenApiInfo { Title = "Optifit API", Version = "v1" }); + option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme + { + In = ParameterLocation.Header, + Description = "Please enter a valid token", + Name = "Authorization", + Type = SecuritySchemeType.Http, + BearerFormat = "JWT", + Scheme = "Bearer" + }); + option.AddSecurityRequirement(new OpenApiSecurityRequirement + { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference + { + Type=ReferenceType.SecurityScheme, + Id="Bearer" + } + }, + new string[]{} + } + }); +}); + +builder.Services.AddDbContext( + options => options.UseInMemoryDatabase("AppDb")); +builder.Services.AddAuthorization(); +builder.Services.AddIdentityApiEndpoints() + .AddEntityFrameworkStores(); + + builder.Services.AddRouting(options => options.LowercaseUrls = true); @@ -70,6 +106,23 @@ if (app.Environment.IsDevelopment()) app.UseHttpsRedirection(); } +app.MapIdentityApi(); +app.MapSwagger().RequireAuthorization(); + app.UseAuthorization(); app.MapControllers(); + +app.MapPost("/logout", async (SignInManager signInManager, + [FromBody]object empty) => + { + if (empty != null) + { + await signInManager.SignOutAsync(); + return Results.Ok(); + } + return Results.Unauthorized(); + }) + .WithOpenApi() + .RequireAuthorization(); + app.Run(); \ No newline at end of file diff --git a/Server/Server.csproj b/Server/Server.csproj index 676f13f..2cf6cf2 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -13,6 +13,7 @@ + all diff --git a/Shared/Shared.csproj b/Shared/Shared.csproj index 0e642cd..09538ab 100644 --- a/Shared/Shared.csproj +++ b/Shared/Shared.csproj @@ -11,4 +11,8 @@ + + + +