Search code examples
reactjsif-statementaxioshttp-posthttpresponse

axios post request in if condition has no response data react.js


I want api to send otp but when default_2fa is mobile or email.write this code but respone is null even status code is 200 , i do some test on otpcall outside of if statement and the response work well

    const login = (values) => {

     let payload = {
        "input": values.phone_number,
        "password": values.password,
        "user_agent":"postman",
        "client_host":"local"
      }
      axios.post(loginUrl, payload = payload)
          .then(resp => {
            
              if (typeof resp !== 'undefined') {
                   if (resp.data.result.is_2fa_active) {
                    if (resp.data.result.default_2fa === "authenticator_app") {
                        setAuthenticatorApp(true)
                    }
                    if ( resp.data.result.default_2fa === "mobile" || resp.data.result.default_2fa === "email") {
                        let payloadOtp = {
                            "type": resp.data.result.default_2fa,
                            "input": values.phone_number,
                            "password": values.password,
                        }
                         axios.post(otpUrl, payloadOtp).then(response => {
                             setEmailOrSmsAuth(true)
                             return response
                        })
                    }
                  }
              }
          })


  }

when i call this api the otp api statuse code is 200 and payload has all values i want but response is null please help me what is wrong with my code!


Solution

  • I realize that this problem comes from the server. Below is my solution to fix this.

    CODE

    const login = (values) => {
    
        setIsLoading(true);
    
        sessionStorage.setItem('mobile', values.phone_number);  //set mobile into local session
    
        sessionStorage.setItem('password', values.password);  //set password into local session
    
        const loginUrl = `${BASE_URL(IAM_APP)}/auth/login/`
    
        let payload = {
    
            "input": values.phone_number,
    
            "password": values.password,
    
            "user_agent": "postman",
    
            "client_host": "local"
    
        }
    
        axios.post(loginUrl, payload)   //request for login if 2factor authentication is true then send to another component
    
            .then(resp => {
    
                if (resp !== undefined){
                    
                    if (resp.data?.result?.access_token) {
    
                    setToken(resp.data.result)
    
                    setTimeout(() => {
    
                        window.location.href = DASHBOARD
    
                    }, 1000);
    
                } else if (resp?.data?.result.is_2fa_active === true) {
    
                    if (resp.data.result.default_2fa === "authenticator_app") {
    
                        history.push(LOGIN2FAAUTHENTICATOR);
                        
                    } else if (resp.data.result.default_2fa === "email") {
    
                        history.push(LOGIN2FAEMAIL);
    
                    } else if (resp.data.result.default_2fa === "mobile") {
    
                        history.push(LOGIN2FASMS);
    
                    }
    
                }
    
            }
    
            })
    
    
    }
    
    then for example in the LOGIN2FASMS route solve issue like this
    
    
    const otpSent = () => {   //send otp to mobile
    
        const otpUrl = `${BASE_URL(IAM_APP)}/auth/2fa/otp/`
    
        let payloadOtp = {
    
            "type": "mobile",
    
            "input": mobile,
    
            "password": password,
    
        }
    
        return axios.post(otpUrl, payloadOtp)
    
    }
    
    useEffect(() => {
    
        otpSent().then(resp => {
    
            if (resp?.data?.result === 'otp sent'){
                
            }
            else
    
                otpSent()
    
            }
    
        )
    
    }, [mobile, password]); 
    
    yeah, its done ;)