Exemplo n.º 1
0
        public async Task <Result> SendEmailAsync(string email)
        {
            var user = await _userQueryRepository.GetUserByEmailAsync(email);

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

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

            var token = _jwtTokenHelper.GenerateTokenWithSecurityCode($"{user.Id}", email, securityCode.Code);

            var url         = $"{_configuration["UiBaseUrl"]}resetpassword?token={token}";
            var htmlMessage =
                $"To reset your password, just click the link below: <a href=\"{HtmlEncoder.Default.Encode(url)}\">clicking here</a>.";

            try
            {
                await _emailSender.SendEmailAsync(email, "Reset your email", htmlMessage);
            }
            catch (Exception e)
            {
                return(Result.Fail <bool>(EC.EmailFailedSend, ET.EmailFailedSend));
            }

            await _unitOfWorks.CommitAsync();

            return(Result.Ok());
        }
Exemplo n.º 2
0
        public async Task <Result> SendSmsCodeAsync(string phoneNumber, string countryCode)
        {
            var isExist = await _userQueryRepository.IsExistPhoneAsync(phoneNumber);

            if (!isExist)
            {
                return(Result.Fail(EC.PhoneNotFound, ET.PhoneNotFound));
            }

            var code = SecurityCode.Create(ProviderType.Phone, phoneNumber, CodeActionType.ForgotPasswordByPhone);

            await _securityCodesRepository.CreateAsync(code);

            await _unitOfWorks.CommitAsync();

            var smsSender = new SmsSender(_globalSettings);
            var result    = await smsSender.SendSmsAsync(phoneNumber, $"{code.Code} is your verification code.");

            if (result == null)
            {
                return(Result.Fail(EC.SmsServiceFailed, ET.SmsServiceFailed));
            }

            return(Result.Ok());
        }
Exemplo n.º 3
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));
        }
Exemplo n.º 4
0
        public async Task <Result> SendSmsAsync(string phoneNumber, string countryCode)
        {
            if (string.IsNullOrEmpty(phoneNumber) || string.IsNullOrEmpty(countryCode))
            {
                return(Result.Fail(EC.PhoneInvalid, ET.PhoneRequired));
            }

            var code = SecurityCode.Create(ProviderType.Phone, phoneNumber, CodeActionType.ConfirmPhone);

            await _securityCodesRepository.CreateAsync(code);

            await _unitOfWorks.CommitAsync();

            var smsSender = new SmsSender(_globalSettings);
            var result    = await smsSender.SendSmsAsync(phoneNumber, $"{code.Code} is your verification code.");

            if (result == null)
            {
                return(Result.Fail(EC.SmsServiceFailed, ET.SmsServiceFailed));
            }

            return(Result.Ok());
        }