I am trying to pass authentication payload to backend api and get the reponse code, while handling the error response code, it is always coming as undefined. We are not able to identify the correct http error code. In first loop, I am able to get the http code response in

catch(function (error) {
      reject(console.log("errorin firstcatch", error.response.status));
      });
    })

however in authorize function the error is coming as undefined.

const authMock = (username, password)=>
  new Promise((resolve, reject)=>{
    let payload ={
      username:username,
      password:password
    }
    // const loginToken=generateJwt(payload);
    // let headers = {
    //   "loginToken" : loginToken
    // }
    axios.post(apiBaseUrl,payload)
    .then(function (response){
      console.log("status", response.data.status_code);
      if(response.status == 200||204){
        resolve({token:response.data.access_token,
                 firstName:response.data.FirstName,
                 lastName:response.data.LastName,
                 profilePic: response.data.image})

      }
      else{
        console.log("login rejected") 
        // reject({ status: 401 });
      }
    })
    .catch(function (error) {
      reject(console.log("errorin firstcatch", error.response.status));
      });
    })

function* authorize({ payload: { username, password } }) {
  try {
    const { token, firstName, lastName, profilePic } = yield call(authMock, username, password);
    yield put({ type: AUTH_SUCCESS, payload: {token, firstName, lastName, profilePic} });
    localStorage.setItem('token', token);
    localStorage.setItem('firstName', firstName);
    localStorage.setItem('lastName', lastName);
    localStorage.setItem("profilePic", profilePic);
  } catch (error) {
    let message;
    console.log("error", error);
    switch (error) {
      case 500: message = 'Internal Server Error'; break;
      case 401: message = 'Wrong user ID'; break;
      case 403: message = "Invalid credentials"; break;
      default: message = "Something went wrong!"; break;
    }
    yield put({ type: AUTH_FAILURE, payload: message });
    localStorage.removeItem('token');
    localStorage.removeItem('firstName');
    localStorage.removeItem('lastName');
    localStorage.removeItem('profilePic');
  }
}

function* Saga() {
  yield takeLatest(AUTH_REQUEST, authorize);
}

1 Answers

0
Tomer Levi On

Inspect this part of your code, it might need a small refactor:

axios.post(apiBaseUrl,payload)
    .then(function (response){
        console.log("status", response.data.status_code);
            if(response.status == 200||204){
                resolve({
                    token:response.data.access_token,
                    firstName:response.data.FirstName,
                    lastName:response.data.LastName,
                    profilePic: response.data.image
                })
            }

      }

You are missing a step by which you'll GET the information you POSTed:

axios.post(apiBaseUrl, payload)
    .then(() => axios.get('URL')) <-- Add another endpoint to get the stored payload
    .then((response) => { <-- Now the response should contain the status
        console.log("status", response.data.status_code);
            if(response.status == 200 || 204){
                resolve({
                    token:response.data.access_token,
                    firstName:response.data.FirstName,
                    lastName:response.data.LastName,
                    profilePic: response.data.image
                })
            }

      }