| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- using Application.Abstractions.Data;
- using Application.Abstractions.Messaging;
- using Application.Abstractions.Messaging.Email;
- 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 (subject, body) = request.Type switch
- {
- VerificationType.Registration => (
- "[bitforum] 회원가입 이메일 인증번호",
- $"<p>회원가입 이메일 인증번호입니다.</p><p><strong>{code}</strong></p><p>이 코드는 5분간 유효합니다.</p>"
- ),
- VerificationType.ForgotPassword => (
- "[bitforum] 비밀번호 재설정 인증번호",
- $"<p>비밀번호 재설정 인증번호입니다.</p><p><strong>{code}</strong></p><p>이 코드는 10분간 유효합니다.</p>"
- ),
- _ => throw new InvalidOperationException($"지원하지 않는 인증 타입입니다: {request.Type}")
- };
- await mailService.SendAsync(new SendData(email, subject, body), ct);
- return Result.Success();
- }
- }
|