Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.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 如何在呈现页面之前检查用户是否经过身份验证?_Reactjs_Redux_React Redux_Next.js - Fatal编程技术网

Reactjs 如何在呈现页面之前检查用户是否经过身份验证?

Reactjs 如何在呈现页面之前检查用户是否经过身份验证?,reactjs,redux,react-redux,next.js,Reactjs,Redux,React Redux,Next.js,我将Next.js与redux和firebase auth一起使用 我试图实现的是在能够访问一些私有页面之前检查用户是否已登录 我已经创建了一个名为AdminLayout的组件,它被包装在每个私有页面上。我使用mapStateToProps获取currentUser作为道具,但在第一次运行时没有获取值 // admin-layout.component.jsx // Imports class AdminLayout extends React.Component { componentD

我将Next.js与redux和firebase auth一起使用

我试图实现的是在能够访问一些私有页面之前检查用户是否已登录

我已经创建了一个名为
AdminLayout
的组件,它被包装在每个私有页面上。我使用
mapStateToProps
获取
currentUser
作为道具,但在第一次运行时没有获取值

// admin-layout.component.jsx
// Imports

class AdminLayout extends React.Component {
  componentDidMount() {
    console.log(this.props.currentUser);
    // if (this.props.currentUser) {
    //   if (this.props.currentUser.currentUser.role != "admin") {
    //     Router.push("/admin/login");
    //   }
    // }
    // if (!this.props.currentUser) {
    //   Router.push("/admin/login");
    // }
  }

  render() {
    return (
      <React.Fragment>
            // Irrelevant code here
            {this.props.children}
            // Irrelevant code here
      </React.Fragment>
    );
  }
}

const mapStateToProps = (state) => ({
  currentUser: state.user.currentUser,
});

export default connect(mapStateToProps)(AdminLayout);
//admin-layout.component.jsx
//进口
类AdminLayout扩展了React.Component{
componentDidMount(){
console.log(this.props.currentUser);
//if(this.props.currentUser){
//if(this.props.currentUser.currentUser.role!=“admin”){
//Router.push(“/admin/login”);
//   }
// }
//如果(!this.props.currentUser){
//Router.push(“/admin/login”);
// }
}
render(){
返回(
//不相关的代码在这里
{this.props.children}
//不相关的代码在这里
);
}
}
常量mapStateToProps=(状态)=>({
currentUser:state.user.currentUser,
});
导出默认连接(MapStateTops)(AdminLayout);

有没有办法克服这种情况,只允许经过身份验证的用户访问私人页面?

但是我使用的是Next.js,所以我不使用react路由器domOk。然后,您可以创建一个用于检查并继续或重定向或执行每个操作的。