Handler.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Application.Abstractions.Messaging.Email;
  4. using Application.Common;
  5. using Domain.Entities.EmailVerification;
  6. using Domain.Entities.EmailVerification.ValueObject;
  7. using SharedKernel.Results;
  8. using Microsoft.EntityFrameworkCore;
  9. namespace Application.Features.Api.Auth.ResendEmail;
  10. internal sealed class Handler(
  11. IAppDbContext db,
  12. IMailService mailService
  13. ) : ICommandHandler<Command, Result>
  14. {
  15. public async Task<Result> Handle(Command request, CancellationToken ct)
  16. {
  17. // 이메일 유효성 검사
  18. if (string.IsNullOrWhiteSpace(request.Email))
  19. {
  20. return Result.Failure(Error.Problem("Auth.EmailRequired", "이메일은 필수입니다."));
  21. }
  22. var email = request.Email.Trim().ToLower();
  23. // 회원 조회
  24. var member = await db.Member.FirstOrDefaultAsync(m => m.Email == email, ct);
  25. if (member is null)
  26. {
  27. return Result.Failure(Error.NotFound("Auth.MemberNotFound", "회원 정보를 찾을 수 없습니다."));
  28. }
  29. // 타입별 유효성 검사
  30. switch (request.Type)
  31. {
  32. case VerificationType.Registration:
  33. if (member.IsEmailVerified)
  34. {
  35. return Result.Failure(Error.Conflict("Auth.AlreadyVerified", "이미 인증된 이메일입니다."));
  36. }
  37. break;
  38. case VerificationType.ForgotPassword:
  39. if (member.IsWithdraw)
  40. {
  41. return Result.Failure(Error.Problem("Auth.MemberWithdrawn", "탈퇴한 회원은 이용할 수 없습니다."));
  42. }
  43. if (member.IsDenied)
  44. {
  45. return Result.Failure(Error.Problem("Auth.MemberDenied", "차단된 회원이므로 이용할 수 없습니다."));
  46. }
  47. break;
  48. }
  49. // 기존 미인증 코드 삭제
  50. var existing = await db.EmailVerifyNumber.Where(e => e.Email == email && e.Type == request.Type && !e.IsVerified).ToListAsync(ct);
  51. db.EmailVerifyNumber.RemoveRange(existing);
  52. // 6자리 랜덤 숫자 코드 생성
  53. var code = Random.Shared.Next(100000, 999999).ToString();
  54. var expiration = request.Type == VerificationType.Registration ? DateTime.UtcNow.AddMinutes(5) : DateTime.UtcNow.AddMinutes(10);
  55. // 인증번호 생성
  56. var verifyNumber = EmailVerifyNumber.Create(request.Type, email, code, expiration);
  57. await db.EmailVerifyNumber.AddAsync(verifyNumber, ct);
  58. await db.SaveChangesAsync(ct);
  59. // 타입별 이메일 발송
  60. var emailTpl = (await db.Config.AsNoTracking().FirstOrDefaultAsync(ct))?.EmailTemplate;
  61. string subject;
  62. string body;
  63. switch (request.Type)
  64. {
  65. case VerificationType.Registration:
  66. subject = string.IsNullOrWhiteSpace(emailTpl?.RegisterEmailFormTitle)
  67. ? "회원가입 이메일 인증번호"
  68. : emailTpl!.RegisterEmailFormTitle!;
  69. body = string.IsNullOrWhiteSpace(emailTpl?.RegisterEmailFormContent)
  70. ? $"<p>회원가입 이메일 인증번호입니다.</p><p><strong>{code}</strong></p><p>이 코드는 5분간 유효합니다.</p>"
  71. : emailTpl!.RegisterEmailFormContent!.Render(("Code", code), ("Email", email));
  72. break;
  73. case VerificationType.ForgotPassword:
  74. subject = string.IsNullOrWhiteSpace(emailTpl?.ResetPasswordEmailFormTitle)
  75. ? "비밀번호 재설정 인증번호"
  76. : emailTpl!.ResetPasswordEmailFormTitle!;
  77. body = string.IsNullOrWhiteSpace(emailTpl?.ResetPasswordEmailFormContent)
  78. ? $"<p>비밀번호 재설정 인증번호입니다.</p><p><strong>{code}</strong></p><p>이 코드는 10분간 유효합니다.</p>"
  79. : emailTpl!.ResetPasswordEmailFormContent!.Render(("Code", code), ("Email", email));
  80. break;
  81. default:
  82. return Result.Failure(Error.Problem("Auth.UnsupportedVerificationType", $"지원하지 않는 인증 타입입니다: {request.Type}"));
  83. }
  84. await mailService.SendAsync(new SendData(email, subject, body), ct);
  85. return Result.Success();
  86. }
  87. }