| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using Application.Abstractions.Data;
- using Application.Abstractions.Messaging;
- using Application.Abstractions.Messaging.Email;
- using Application.Common;
- using Domain.Entities.EmailVerification;
- using Domain.Entities.EmailVerification.ValueObject;
- using SharedKernel.Results;
- using Microsoft.EntityFrameworkCore;
- namespace Application.Features.Api.Auth.ResendEmail;
- internal sealed class Handler(
- IAppDbContext db,
- IMailService mailService
- ) : ICommandHandler<Command, Result>
- {
- public async Task<Result> Handle(Command request, CancellationToken ct)
- {
- // 이메일 유효성 검사
- if (string.IsNullOrWhiteSpace(request.Email))
- {
- return Result.Failure(Error.Problem("Auth.EmailRequired", "이메일은 필수입니다."));
- }
- var email = request.Email.Trim().ToLower();
- // 회원 조회
- var member = await db.Member.FirstOrDefaultAsync(m => m.Email == email, ct);
- if (member is null)
- {
- return Result.Failure(Error.NotFound("Auth.MemberNotFound", "회원 정보를 찾을 수 없습니다."));
- }
- // 타입별 유효성 검사
- switch (request.Type)
- {
- case VerificationType.Registration:
- if (member.IsEmailVerified)
- {
- return Result.Failure(Error.Conflict("Auth.AlreadyVerified", "이미 인증된 이메일입니다."));
- }
- break;
- case VerificationType.ForgotPassword:
- if (member.IsWithdraw)
- {
- return Result.Failure(Error.Problem("Auth.MemberWithdrawn", "탈퇴한 회원은 이용할 수 없습니다."));
- }
- if (member.IsDenied)
- {
- return Result.Failure(Error.Problem("Auth.MemberDenied", "차단된 회원이므로 이용할 수 없습니다."));
- }
- break;
- }
- // 기존 미인증 코드 삭제
- var existing = await db.EmailVerifyNumber.Where(e => e.Email == email && e.Type == request.Type && !e.IsVerified).ToListAsync(ct);
- db.EmailVerifyNumber.RemoveRange(existing);
- // 6자리 랜덤 숫자 코드 생성
- var code = Random.Shared.Next(100000, 999999).ToString();
- var expiration = request.Type == VerificationType.Registration ? DateTime.UtcNow.AddMinutes(5) : DateTime.UtcNow.AddMinutes(10);
- // 인증번호 생성
- var verifyNumber = EmailVerifyNumber.Create(request.Type, email, code, expiration);
- await db.EmailVerifyNumber.AddAsync(verifyNumber, ct);
- await db.SaveChangesAsync(ct);
- // 타입별 이메일 발송
- var emailTpl = (await db.Config.AsNoTracking().FirstOrDefaultAsync(ct))?.EmailTemplate;
- string subject;
- string body;
- switch (request.Type)
- {
- case VerificationType.Registration:
- subject = string.IsNullOrWhiteSpace(emailTpl?.RegisterEmailFormTitle)
- ? "회원가입 이메일 인증번호"
- : emailTpl!.RegisterEmailFormTitle!;
- body = string.IsNullOrWhiteSpace(emailTpl?.RegisterEmailFormContent)
- ? $"<p>회원가입 이메일 인증번호입니다.</p><p><strong>{code}</strong></p><p>이 코드는 5분간 유효합니다.</p>"
- : emailTpl!.RegisterEmailFormContent!.Render(("Code", code), ("Email", email));
- break;
- case VerificationType.ForgotPassword:
- subject = string.IsNullOrWhiteSpace(emailTpl?.ResetPasswordEmailFormTitle)
- ? "비밀번호 재설정 인증번호"
- : emailTpl!.ResetPasswordEmailFormTitle!;
- body = string.IsNullOrWhiteSpace(emailTpl?.ResetPasswordEmailFormContent)
- ? $"<p>비밀번호 재설정 인증번호입니다.</p><p><strong>{code}</strong></p><p>이 코드는 10분간 유효합니다.</p>"
- : emailTpl!.ResetPasswordEmailFormContent!.Render(("Code", code), ("Email", email));
- break;
- default:
- return Result.Failure(Error.Problem("Auth.UnsupportedVerificationType", $"지원하지 않는 인증 타입입니다: {request.Type}"));
- }
- await mailService.SendAsync(new SendData(email, subject, body), ct);
- return Result.Success();
- }
- }
|