Profile.cs 990 B

123456789101112131415161718192021222324252627282930313233343536
  1. using System.Security.Claims;
  2. using MediatR;
  3. using Web.Api.Extensions;
  4. using Web.Api.Common;
  5. using Microsoft.IdentityModel.JsonWebTokens;
  6. namespace Web.Api.Endpoints.Auth;
  7. internal sealed class Profile : IEndpoint
  8. {
  9. public void MapEndpoint(IEndpointRouteBuilder app)
  10. {
  11. app.MapGet("api/auth/profile", async (
  12. ClaimsPrincipal user,
  13. ISender sender,
  14. CancellationToken ct
  15. ) => {
  16. var memberIDClaim = user.FindFirst(JwtRegisteredClaimNames.Sub)?.Value;
  17. if (!int.TryParse(memberIDClaim, out var memberID))
  18. {
  19. return Results.Unauthorized();
  20. }
  21. var query = new Application.Features.Auth.GetProfile.Query(memberID);
  22. var result = await sender.Send(query, ct);
  23. return result.Match(
  24. Results.Ok,
  25. CustomResults.Problem
  26. );
  27. })
  28. .WithTags("Auth")
  29. .RequireAuthorization();
  30. }
  31. }