From 0e808cd004cd805dd141846497ca2556b9fc8096 Mon Sep 17 00:00:00 2001 From: Louis LABORIE Date: Thu, 6 Feb 2025 13:59:17 +0100 Subject: [PATCH] Implement login method :seedling::seedling: --- Infrastructure/ApplicationDbContext.cs | 13 ++++++ Infrastructure/Infrastructure.csproj | 2 + Server/FirstTest.db | Bin 0 -> 12288 bytes Server/Program.cs | 59 +++++++++++++++++++++++-- Server/Server.csproj | 1 + Shared/Shared.csproj | 4 ++ 6 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 Infrastructure/ApplicationDbContext.cs create mode 100644 Server/FirstTest.db 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 0000000000000000000000000000000000000000..2fa92464e4e2c2e71e38f9d6c26e57da4bfd06d8 GIT binary patch literal 12288 zcmeI#%}T>S5C`zxC@O(|+Br-Td@ZT(WDZn4JKQtM|~e3r!V0X zcyzZ?4-%>8@*kKDlWb?dLM1Nl`u}lFnG0an8<(7-L-Jj>>iDdUsu@tFAW% zXD5@7M(dLq%_VCtTko4*z;g&d00Izz00bZa0SG_<0uX?}zXe`Le7|Gc{LPce{WObX z_p+ZqmWj-ZS#_0HvM66_ZyMdmb%aa8x$s@GYUfyVXp#!+r=^!#B-~p;!9eB6_o+Jw zh9YvjK#&#o;y?O?k=J*kG4@psFxEYkUdyvfQ+ed@2S=8!ZKs^_UX zxyQ*|7OT2`mH4!Faeb~o(4GxFwDo|100bZa0SG_<0uX=z1Rwwb2tZ(41x&p$od37= b_hMfVfB*y_009U<00Izz00bZaftA1)AlGZA literal 0 HcmV?d00001 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 @@ + + + +