Logout.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System.Security.Claims;
  2. using MediatR;
  3. using Web.Api.Common;
  4. using Web.Api.Extensions;
  5. namespace Web.Api.Endpoints.Auth;
  6. internal sealed class Logout : IEndpoint
  7. {
  8. public void MapEndpoint(IEndpointRouteBuilder app)
  9. {
  10. app.MapPost("api/auth/logout", async (
  11. ClaimsPrincipal user,
  12. ISender sender,
  13. HttpContext httpContext,
  14. CancellationToken ct
  15. ) => {
  16. var memberID = user.GetMemberID();
  17. if (memberID is null)
  18. {
  19. return ApiResponse.Fail(StatusCodes.Status401Unauthorized, "Invalid token");
  20. }
  21. var refreshToken = httpContext.Request.Cookies["refreshToken"];
  22. var command = new Application.Features.Api.Auth.Logout.Command(memberID.Value, refreshToken);
  23. var result = await sender.Send(command, ct);
  24. return result.Match(
  25. () => ApiResponse.Ok(),
  26. CustomResults.Problem
  27. );
  28. })
  29. .WithTags("Auth")
  30. .RequireAuthorization();
  31. }
  32. }