VerifyEmail.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using MediatR;
  2. using Web.Api.Extensions;
  3. using Web.Api.Common;
  4. using Domain.Entities.EmailVerification.ValueObject;
  5. namespace Web.Api.Endpoints.Auth;
  6. internal sealed class VerifyEmail : IEndpoint
  7. {
  8. public sealed record Request(string Email, string Code, VerificationType Type);
  9. public void MapEndpoint(IEndpointRouteBuilder app)
  10. {
  11. // 이메일 인증번호 검증
  12. app.MapPost("api/auth/verify-email", async (
  13. Request request,
  14. HttpContext httpContext,
  15. ISender sender,
  16. CancellationToken ct
  17. ) =>
  18. {
  19. var command = new Application.Features.Api.Auth.VerifyEmail.Command(
  20. request.Email,
  21. request.Code,
  22. request.Type
  23. );
  24. var result = await sender.Send(command, ct);
  25. if (result.IsSuccess)
  26. {
  27. // 인증 타입별 쿠키 설정
  28. var cookieOptions = new CookieOptions
  29. {
  30. HttpOnly = true,
  31. Secure = true,
  32. SameSite = SameSiteMode.None,
  33. Expires = result.Value switch
  34. {
  35. VerificationType.Registration => DateTime.UtcNow.AddMinutes(5),
  36. VerificationType.ForgotPassword => DateTime.UtcNow.AddMinutes(10),
  37. _ => DateTime.UtcNow.AddMinutes(5)
  38. }
  39. };
  40. // 사전 인증 완료 쿠키 설정 (예: isVerified-Registration, isVerified-ForgotPassword)
  41. httpContext.Response.Cookies.Append(
  42. $"isVerified-{result.Value}",
  43. "true",
  44. cookieOptions
  45. );
  46. }
  47. return result.Match(
  48. _ => ApiResponse.Ok(new { message = "이메일 인증 성공" }),
  49. CustomResults.Problem
  50. );
  51. })
  52. .WithTags("Auth")
  53. .AllowAnonymous();
  54. }
  55. }