失败后无法发送行动

时间:2018-05-15 12:23:14

标签: javascript reactjs authentication redux

我在我的应用程序上实现了身份验证,问题是当我尝试在失败后登录时,例如,如果密码错误..

这是登录功能:

export const signIn = data => dispatch => {
  dispatch({ 
    type: SIGN_IN
    }) 
  fetch(API_URL+'/login', { 
   method: 'POST',
   headers: {
      'content-type': 'application/json'
      },
   body: JSON.stringify(data),
    }).then((response) => {
  if (response.ok) {
    return response.json();
  } else {
    throw new Error('Something went wrong');
  }
}).then((responseJson) => {
      dispatch({
          type: SIGN_IN_SUCCESS, payload: responseJson
        }),
        localStorage.setItem("token", 'Bearer '+responseJson.token)
        localStorage.setItem("user", JSON.stringify(responseJson.user))
    })
    .catch((error) => {
      dispatch({
          type: SIGN_IN_FAILED, payload: error
        })  
    });
}

0 个答案:

没有答案