0

I have two headers, the first one contains APP_TOKEN and the second contains APP_TOKEN and USER_TOKEN, how do I condition if my request requires only APP_TOKEN or APP_TOKEN and USER_TOKEN on axios

this is my axios

import axios from 'axios'

export default axios.create((param_app){
  if(param_app == APP_TOKEN){
    baseURL: 'http://exampe',
    headers: {
      'timeout' : 30000,
      'APP_TOKEN': 'apptoken_example',
    }
  }else if(param_app === APP_TOKEN && USER_TOKEN){
    baseURL: 'http://exampe',
    headers: {
      'timeout' : 30000,
      'APP_TOKEN': 'apptoken_example',
      'USER_TOKEN': JSON.parse(localStorage.getItem('data')).TOKEN
    }
  }
})

for example this login page only requires APP_TOKEN for headers

async handleSubmit(e){
    e.preventDefault()
    if(this.state.MEMBER_EMAIL && this.state.MEMBER_PASSWORD){
      const headers = {
        'timeout' : 30000,
        'APP_TOKEN': 'example',
      }
      await API.post('member/login', this.state ,{headers})
      .then((response) => {
        let responseJson = response
        if(responseJson.data.STATUS_CODE === '200'){
          this.props.resSave('data', JSON.stringify(responseJson.data.DATA))
          this.setState({
            redirect: true
          })
        }else if(responseJson.data.STATUS_CODE === '400') {
          alert(responseJson.data.MESSAGE)
          this.setState({
            redirect: false
          })
        }
      })
    }
  }

and this is an example of a page that requires APP_TOKEN and USER_TOKEN

async componentDidMount() {
    const headers = {
      'timeout' : 30000,
      'APP_TOKEN': 'example',
      'USER_TOKEN': JSON.parse(localStorage.getItem('data')).TOKEN
    }
    await API.get("/banner-home", {headers})
    .then(response=>this.setState({
      banner:response.data.DATA,
      loading:false
    }))
  }

1 Answers1

0

You need to switch around the conditions in the first code block. You first want to check that both values are there and if that is not the case check if only the APP_TOKEN is there. Currently, if APP_TOKEN token is present if will never reach the second test.

Martijn
  • 72
  • 2