| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using Application.Abstractions.Data;
- using Application.Abstractions.Cache;
- using Application.Abstractions.Messaging;
- using Application.Abstractions.Messaging.Email;
- using Application.Common;
- using Application.Features.Config.Get;
- using Application.Helpers;
- 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);
- }
- // 가입 완료 환영 이메일 발송
- var emailTpl = await AccountConfigLoader.GetEmailTemplateAsync(cache, db, ct);
- var subject = string.IsNullOrWhiteSpace(emailTpl.RegistrationEmailFormTitle)
- ? "회원가입을 환영합니다"
- : emailTpl.RegistrationEmailFormTitle;
- var content = string.IsNullOrWhiteSpace(emailTpl.RegistrationEmailFormContent)
- ? $"<p>회원가입이 완료되었습니다.</p><p>서비스를 이용해 주셔서 감사합니다.</p>"
- : emailTpl.RegistrationEmailFormContent.Render(("Email", email), ("Nickname", member.Name ?? string.Empty), ("CreatedAt", member.CreatedAt.AddHours(9).ToString("yyyy-MM-dd HH:mm")));
- await mailService.SendAsync(new SendData(email, subject, content), ct);
- return Result.Success();
- }
- }
|