Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs react redux中的授权。容器组件在解码令牌设置为存储之前接收道具(isLoggedIn)_Reactjs_React Redux - Fatal编程技术网

Reactjs react redux中的授权。容器组件在解码令牌设置为存储之前接收道具(isLoggedIn)

Reactjs react redux中的授权。容器组件在解码令牌设置为存储之前接收道具(isLoggedIn),reactjs,react-redux,Reactjs,React Redux,当用户访问站点时,我想检查localStorage中的jwtToken,如果它有效,用户将被重定向到主页 但ContainerComponent在解码令牌并设置要存储的数据之前接收道具(使用isLoggedInprop)。我能修一下吗 下面是我检查令牌的代码: index.js document.addEventListener("DOMContentLoaded", async () => { const token = localStorage.getItem(

当用户访问站点时,我想检查localStorage中的jwtToken,如果它有效,用户将被重定向到主页

但ContainerComponent在解码令牌并设置要存储的数据之前接收道具(使用isLoggedInprop)。我能修一下吗

下面是我检查令牌的代码:
index.js

document.addEventListener("DOMContentLoaded", async () => {
  const token = localStorage.getItem("jwtToken");
  const store = configureStore();
  //Check for token
  if (token) {
    // Set auth token header auth
    API.setAuthToken(token);
    // Decode token and get user info and exp
    const decoded = await jwt_decode(token);
    // Check for expired token
    const currentTime = Date.now() / 1000;
    if (decoded.exp < currentTime) {
      // Logout user
      await store.dispatch(logoutUser());
      // Redirect to login
      window.location.href = "/auth";
    } else {
      await store.dispatch(setCurrentUser(token));
    }
  }

  ReactDOM.render(
    <Router>
      <Provider store={store}>
        <App />
      </Provider>
    </Router>,
    document.getElementById("root")
  );
});
我检查页面容器组件中的isLoggedIn

import AuthPage from "./authPage";
import { authSuccess } from "../../actions/session.actions";
import { authUser } from "../../actions/session.actions";

class AuthPageContainer extends React.Component {
  render() {
    return (
      <>
        <AuthPage
          authSuccess={this.props.authSuccess}
          authUser={this.props.authUser}
          isLoggedIn={this.props.isLoggedIn}
        />
      </>
    );
  }
}

const mapStateToProps = (state) => {
  return {
    isLoggedIn: Boolean(state.Session),
  };
};

export default connect(mapStateToProps, { authUser, authSuccess })(
  AuthPageContainer
);

从“/AuthPage”导入AuthPage;
从“../../actions/session.actions”导入{authSuccess};
从“../../actions/session.actions”导入{authUser};
类AuthPageContainer扩展了React.Component{
render(){
返回(
);
}
}
常量mapStateToProps=(状态)=>{
返回{
isLoggedIn:Boolean(state.Session),
};
};
导出默认连接(MapStateTops,{authUser,authSuccess})(
AuthPageContainer
);

我解决了那个问题!我不是分派thunk函数,而是分派普通对象

之前:
store.dispatch(setCurrentUser(令牌))
之后:
store.dispatch(authSuccess(令牌))

import AuthPage from "./authPage";
import { authSuccess } from "../../actions/session.actions";
import { authUser } from "../../actions/session.actions";

class AuthPageContainer extends React.Component {
  render() {
    return (
      <>
        <AuthPage
          authSuccess={this.props.authSuccess}
          authUser={this.props.authUser}
          isLoggedIn={this.props.isLoggedIn}
        />
      </>
    );
  }
}

const mapStateToProps = (state) => {
  return {
    isLoggedIn: Boolean(state.Session),
  };
};

export default connect(mapStateToProps, { authUser, authSuccess })(
  AuthPageContainer
);