Esempio n. 1
0
        public ActionResult <LoginFormResponse> Post([FromBody] OtpLoginForm otpLoginForm)
        {
            LoginFormResponse loginFormResponse = _userBiz.AuthenticateUserByMobileNumber(otpLoginForm);

            return(Ok(loginFormResponse));
        }
        public LoginFormResponse AuthenticateUserByMobileNumber(OtpLoginForm otpLoginForm)
        {
            LoginFormResponse otpLoginFormResponse = new LoginFormResponse()
            {
                StatusCode = 200
            };

            if (otpLoginForm == null)
            {
                otpLoginFormResponse.StatusCode = 400;
                otpLoginFormResponse.ErrorList  = new List <LoginValidationResponse>()
                {
                    new LoginValidationResponse()
                    {
                        Code    = 1001,
                        Message = "Request Object can't be blank."
                    }
                };
                return(otpLoginFormResponse);
            }

            if (string.IsNullOrWhiteSpace(otpLoginForm.MobileNumber))
            {
                otpLoginFormResponse.StatusCode = 400;
                otpLoginFormResponse.ErrorList  = new List <LoginValidationResponse>()
                {
                    new LoginValidationResponse()
                    {
                        Code    = 1001,
                        Message = "MobileNumber can't be blank."
                    }
                };
                return(otpLoginFormResponse);
            }

            if (string.IsNullOrWhiteSpace(otpLoginForm.Otp))
            {
                otpLoginFormResponse.StatusCode = 400;
                otpLoginFormResponse.ErrorList  = new List <LoginValidationResponse>()
                {
                    new LoginValidationResponse()
                    {
                        Code    = 1001,
                        Message = "Otp can't be blank."
                    }
                };
                return(otpLoginFormResponse);
            }

            if (otpLoginForm.CountryCode <= 0)
            {
                otpLoginFormResponse.StatusCode = 400;
                otpLoginFormResponse.ErrorList  = new List <LoginValidationResponse>()
                {
                    new LoginValidationResponse()
                    {
                        Code    = 1001,
                        Message = "CountryCode can't be blank."
                    }
                };
                return(otpLoginFormResponse);
            }

            if (!Regex.IsMatch(otpLoginForm.MobileNumber, RegexPattern.mobile_number_validation_Patterns.GetCombinedPattern()))
            {
                otpLoginFormResponse.StatusCode = 400;
                otpLoginFormResponse.ErrorList  = new List <LoginValidationResponse>()
                {
                    new LoginValidationResponse()
                    {
                        Code      = 1001,
                        FieldName = nameof(otpLoginForm.MobileNumber),
                        Message   = nameof(otpLoginForm.MobileNumber) + " should be valid. Format -: xxxxxxxxxx "
                    }
                };
                return(otpLoginFormResponse);
            }

            bool isMobileNumberExist = _userLayer.isExistPhoneNumber(otpLoginForm.MobileNumber);

            if (isMobileNumberExist)
            {
                SmsApiResponse smsApiResponse = _smsBiz.VerifyOtpSms(new VerifyOtpRequest()
                {
                    Mobile      = otpLoginForm.MobileNumber,
                    Otp         = otpLoginForm.Otp,
                    CountryCode = otpLoginForm.CountryCode
                });

                if (smsApiResponse.StatusCode == 200)
                {
                    UserInformation userInformation = _userLayer.GetUserDetailByMobileNumber(otpLoginForm.MobileNumber);
                    if (userInformation != null)
                    {
                        JwtTokenResponse jwtTokenResponse = GetJWTToken(userInformation);
                        if (jwtTokenResponse.StatusCode == 200)
                        {
                            JwtToken tokenDetail = jwtTokenResponse.TokenDetail;
                            otpLoginFormResponse.StatusCode      = 200;
                            otpLoginFormResponse.Response        = new LoginResponse();
                            otpLoginFormResponse.Response.UserId = userInformation.UserId;
                            if (tokenDetail != null)
                            {
                                otpLoginFormResponse.Response.TokenDetail = new Token()
                                {
                                    Type      = tokenDetail.Type,
                                    Value     = tokenDetail.Value,
                                    CreatedAt = tokenDetail.CreatedAt,
                                    ExpiredAt = tokenDetail.ExpiredAt
                                };
                            }
                        }
                        else
                        {
                            otpLoginFormResponse.StatusCode = 400;
                            otpLoginFormResponse.ErrorList  = jwtTokenResponse
                                                              .ErrorList
                                                              .Select(x => new LoginValidationResponse()
                            {
                                FieldName = x.FieldName,
                                Message   = x.Message,
                                Code      = x.Code
                            }).ToList();
                        }
                    }
                }
                else
                {
                    otpLoginFormResponse.StatusCode = 400;
                    otpLoginFormResponse.ErrorList  = smsApiResponse.ErrorList.Select(x => new LoginValidationResponse()
                    {
                        Code    = x.Code,
                        Message = x.Message
                    }).ToList();
                }
            }
            else
            {
                otpLoginFormResponse.StatusCode = 400;
                otpLoginFormResponse.ErrorList  = new List <LoginValidationResponse>()
                {
                    new LoginValidationResponse()
                    {
                        Code    = 1001,
                        Message = nameof(otpLoginForm.MobileNumber) + " not found."
                    }
                };
            }

            return(otpLoginFormResponse);
        }