Exemplo n.º 1
0
        public async Task <IActionResult> ResendOtp([FromBody] GetOTP getOtp)
        {
            int userId = 0;
            var otp    = new GenerateOtp();

            if (string.IsNullOrEmpty(getOtp.PhoneNumber))
            {
                return(BadRequest(new
                {
                    Response = new JsonResponseHandler
                    {
                        IsSuccess = false,
                        ErrorMessage = AppStrings.PhoneNumberOrCountryCodeEmpty
                    }
                }));
            }

            // Generating OTP Passcode
            var otpPassCode = otp.GetOtpNumber();
            var otpSms      = AppStrings.ConnectApi + otpPassCode;

            // Get Number Of OTP Attemps
            int numberOfAttempts = await _service.GetNumberOfAttempts(getOtp.PhoneNumber);

            //Get UserID with PhoneNumber
            var user = await _service.GetUserByPhoneNumber(getOtp.PhoneNumber);

            if (user != null)
            {
                userId = user.Id;
            }

            //Get UserOTP with PhoneNumber
            var userOtp = await _service.GetUserOtpInfoByPhoneNumber(getOtp.PhoneNumber);

            // Exceeded OTP Attempts Response
            if (numberOfAttempts > AppConstants.OtpNumberOfAttempts ||
                userOtp.CreatedDate.AddMinutes(AppConstants.DurationUntilOtpCodeActive) < DateTime.UtcNow)
            {
                return(BadRequest(new
                {
                    Response = new JsonResponseHandler
                    {
                        ErrorMessage = AppStrings.OtpCodeAttemptsExceeded,
                        IsSuccess = false
                    }
                }));
            }

            ++numberOfAttempts;

            //To Save Otp Code Against PhoneNumber
            await _service.SaveResentOtpAgainstPhoneNumber(otpPassCode, getOtp.PhoneNumber, numberOfAttempts, userId);

            //Send OTP Code To Mobile
            var twilioSmsHelper = new TwilioSmsSendHelper();
            var responseTwilio  = await twilioSmsHelper.SendSms(otpSms, getOtp.PhoneNumber);

            return(Ok(new
            {
                Code = otpPassCode,
                Response = new JsonResponseHandler
                {
                    IsSuccess = true,
                    ErrorMessage = responseTwilio
                }
            }));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> SendOtpCode([FromBody] GetOTP getOtp)
        {
            int userId           = 0;
            int numberOfAttempts = 1;

            var otp = new GenerateOtp();

            if (string.IsNullOrEmpty(getOtp.PhoneNumber))
            {
                return(BadRequest(new
                {
                    Response = new JsonResponseHandler
                    {
                        IsSuccess = false,
                        ErrorMessage = AppStrings.PhoneNumberOrCountryCodeEmpty
                    }
                }));
            }

            // Check if Phone Number is Digits Only
//            var checkPhoneNumberCorrect = _service.IsDigitsOnly(getOtp.PhoneNumber);
//            if (!checkPhoneNumberCorrect)
//            {
//                return BadRequest(new
//                {
//                    Response = new JsonResponseHandler
//                    {
//                        IsSuccess = false,
//                        ErrorMessage = AppStrings.InvalidPhoneNumberOrCountryCode,
//                    }
//                });
//            }


            // Generating OTP Passcode
            var otpPassCode = otp.GetOtpNumber();
            var otpSms      = AppStrings.ConnectApi + otpPassCode;

            //Get UserID with PhoneNumber
            var user = await _service.GetUserByPhoneNumber(getOtp.PhoneNumber);

            if (user != null)
            {
                userId = user.Id;
            }


            //To Save Otp Code Against PhoneNumber
            await _service.SaveGeneratedOtpAgainstPhoneNumber(otpPassCode, getOtp.PhoneNumber, numberOfAttempts,
                                                              userId);

            //Send OTP Code To Mobile
            var twilioSmsHelper = new TwilioSmsSendHelper();
            var responseTwilio  = await twilioSmsHelper.SendSms(otpSms, getOtp.PhoneNumber);


            return(Ok(new
            {
                Code = otpPassCode,
                Response = new JsonResponseHandler
                {
                    IsSuccess = true,
                    ErrorMessage = responseTwilio,
                }
            }));
        }