Handler.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Cache;
  3. using Application.Abstractions.Messaging;
  4. using Application.Abstractions.Messaging.Email;
  5. using Application.Features.Config.Get;
  6. using SharedKernel.Results;
  7. using Microsoft.EntityFrameworkCore;
  8. namespace Application.Features.Api.Auth.Registration;
  9. internal sealed class Handler(
  10. IAppDbContext db,
  11. ICacheService cache,
  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. // Config에서 IsRegisterEmailAuth 설정 조회
  30. var isRegisterEmailAuth = false;
  31. var config = await cache.GetAsync<Response>(CacheKeys.Config, ct);
  32. if (config is not null)
  33. {
  34. isRegisterEmailAuth = config.Account.IsRegisterEmailAuth;
  35. }
  36. else
  37. {
  38. var configEntity = await db.Config.AsNoTracking().OrderByDescending(x => x.ID).FirstOrDefaultAsync(ct);
  39. if (configEntity is not null)
  40. {
  41. isRegisterEmailAuth = configEntity.Account.IsRegisterEmailAuth;
  42. }
  43. }
  44. // 이메일 인증이 필요한 경우 쿠키 확인 및 이메일 인증 처리
  45. if (isRegisterEmailAuth)
  46. {
  47. if (string.IsNullOrWhiteSpace(request.CookieValue) || request.CookieValue != "true")
  48. {
  49. return Result.Failure(Error.Problem("Auth.VerificationRequired", "사전 인증을 먼저 수행하세요."));
  50. }
  51. // 이메일 인증 완료 처리
  52. member.MarkEmailVerified();
  53. await db.SaveChangesAsync(ct);
  54. }
  55. // 가입 완료 환영 이메일 발송
  56. await mailService.SendAsync(new SendData(
  57. email,
  58. "[bitforum] 회원가입을 환영합니다",
  59. $"<p>회원가입이 완료되었습니다.</p><p>bitforum을 이용해 주셔서 감사합니다.</p>"
  60. ), ct);
  61. return Result.Success();
  62. }
  63. }