Handler.cs 3.5 KB

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