如何使用Redux对多个请求实施身份验证

时间:2019-03-31 09:24:42

标签: javascript reactjs redux jwt

我可以登录,当我刷新浏览器时,登录为unauth。

我试图获取userinfo,当我刷新浏览器时,我丢失了第二个api数据

    axios
     .get("http://AGCorp-WS19:4000/global/WindowsAuth",
     {
      params: userData
     })
  .then(res => {
  if (res.data.authrization === "authorized") {
    const { token } = res.data;
    localStorage.setItem("jwt", token);
    setAuthToken(token);
    // Decode token to get user data
    const decoded = jwt_decode(token);
    // Set current user
    console.log(decoded);
    dispatch(setCurrentUser(decoded));
    return axios
      .get(" http://agcorp-ws19:4000/hr/tblEmployee", {
        params: { strOfficeEmail: userData.username }
      })
      .then(res => {
        console.log(res.data);
        dispatch({
          type: SET_CURRENT_USER,
          payload: res.data
        });
      });
  } else {
    console.log(res.data);
  }
})
.catch(function(error) {
  console.log(error);
});
};

// Set logged in user

  export const setCurrentUser = decoded => {

return {

type: GET_USER_EMAIL,

 payload: decoded


   };
  };
   index.js

   if (localStorage.jwt) {

   setAuthToken(localStorage.jwt);

   const decoded = jwt_decode(localStorage.jwt);

   store.dispatch(setCurrentUser(decoded));

   }

当我提交用户名和密码时,我得到了{token: 'asdfasd", authorized:"auth"},然后用户名将转到第二个api并提供给我大量数据。

例如,UserInformation。我得到了第一个数据,但是当我刷新时,我丢失了用户信息,并且登录名是unauth。

0 个答案:

没有答案