| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using Application.Abstractions.Data;
- using Application.Abstractions.Cache;
- using Application.Abstractions.Messaging;
- using Application.Abstractions.Messaging.Email;
- using Application.Features.Config.Get;
- using SharedKernel.Results;
- using Microsoft.EntityFrameworkCore;
- namespace Application.Features.Api.Auth.Registration;
- internal sealed class Handler(
- IAppDbContext db,
- ICacheService cache,
- 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", "등록된 회원 정보를 찾을 수 없습니다."));
- }
- // Config에서 IsRegisterEmailAuth 설정 조회
- var isRegisterEmailAuth = false;
- var config = await cache.GetAsync<Response>(CacheKeys.Config, ct);
- if (config is not null)
- {
- isRegisterEmailAuth = config.Account.IsRegisterEmailAuth;
- }
- else
- {
- var configEntity = await db.Config.AsNoTracking().OrderByDescending(x => x.ID).FirstOrDefaultAsync(ct);
- if (configEntity is not null)
- {
- isRegisterEmailAuth = configEntity.Account.IsRegisterEmailAuth;
- }
- }
- // 이메일 인증이 필요한 경우 쿠키 확인 및 이메일 인증 처리
- if (isRegisterEmailAuth)
- {
- if (string.IsNullOrWhiteSpace(request.CookieValue) || request.CookieValue != "true")
- {
- return Result.Failure(Error.Problem("Auth.VerificationRequired", "사전 인증을 먼저 수행하세요."));
- }
- // 이메일 인증 완료 처리
- member.MarkEmailVerified();
- await db.SaveChangesAsync(ct);
- }
- // 가입 완료 환영 이메일 발송
- await mailService.SendAsync(new SendData(
- email,
- "[bitforum] 회원가입을 환영합니다",
- $"<p>회원가입이 완료되었습니다.</p><p>bitforum을 이용해 주셔서 감사합니다.</p>"
- ), ct);
- return Result.Success();
- }
- }
|