Ejemplo n.º 1
0
        public async Task <Result <bool> > SendEmailConfirmEmailAsync(int userId)
        {
            var user = await _userQueryRepository.GetUserByIdAsync(userId);

            if (user == null)
            {
                return(Result.Fail <bool>(EC.UserNotFound, ET.UserNotFound));
            }

            if (user.EmailConfirmed)
            {
                return(Result.Fail <bool>(EC.EmailAlreadyConfirmed, ET.EmailAlreadyConfirmed));
            }

            var securityCode = SecurityCode.Create(ProviderType.Email, user.Email, CodeActionType.ConfirmEmail);
            await _securityCodesRepository.CreateAsync(securityCode);

            var token = _jwtTokenHelper.GenerateTokenWithSecurityCode(userId.ToString(), user.Email, securityCode.Code);

            var url = $"{_configuration["UiBaseUrl"]}confirm-email?token={token}";

            var htmlMessage =
                $"Please confirm your email by <a href=\"{HtmlEncoder.Default.Encode(url)}\">clicking here</a>.";

            await _emailSender.SendEmailAsync(user.Email, "Confirm email", htmlMessage);

            await _unitOfWorks.CommitAsync();

            return(Result.OK(true));
        }