0

我正在尝试将导航栏内容从登录/注册更改为其他内容,例如用户登录后的个人资料。当用户未登录时,我的服务器发送 401 并且我有一个 HOC (RequireAuth.js) 用于检查受保护的路由也是如此,如果他们没有登录,则将它们重定向到登录。但是,我无法想出一种用这种逻辑更改导航栏内容的方法,并且想知道是否有一个好的方法来做到这一点(我有如果可能,不想为此目的使用 Redux)。

RequireAuth.js

const RequireAuth = ( Component ) => {

    return class Apps extends React.Component {
        state = {
            isAuthenticated: false,
            isLoading: true
        }

        checkAuthentication = async() => {
            const url = '/getinfo'
            const json = await fetch(url, {method: 'GET'})
            if (json.status !== 401)    {
                setTimeout(
                function() {
                    this.setState({isAuthenticated: true, isLoading: false});}.bind(this), 1500);
            } else {
                setTimeout(
                function() {
                    this.setState({isLoading: false});}.bind(this), 1500);
            }
        }

        componentDidMount() {
            this.checkAuthentication()
        }

        render() {
          const style = {position: "fixed", top: "50%", left: "50%", transform: "translate(-50%, -50%)" };
            console.log(this.state.isLoading)
           const { isAuthenticated, isLoading } = this.state;
           if(!isAuthenticated) {
               return this.state.isLoading? <div style={style}><PacmanLoader color={'#36D7CC'}/></div> : <Redirect to="/" />
           }
           return <Component {...this.props} />
        }
    }
}

export { RequireAuth }
class App extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    const NotFoundComponent = () => <div>404 NOT FOUND</div>
  return (
    <div>
      <Router>
            <NavigationBar />
            <Switch>
              <Route exact path = '/'
                component = {LandingPage}
              />
              <Route exact path = '/register'
                component = {Register}
              />
              <Route exact path = '/Profile'
                component = {RequireAuth(Profile)}
              />
              <Route exact path = '/About'
                component = {RequireAuth(About)}
              />
              <Route exact path = '/Name'
                component = {RequireAuth(Name)}
              />
              <Route path="*" component = {NotFoundComponent}/>
            </Switch>
      </Router>
    </div>
  );
}
}

export default withRouter(App);

导航.js

class NavigationBar extends React.Component {

  constructor(props) {
    super(props);
  }

render() {
    return (
        <Navbar bg="dark" variant="dark" expand="lg">
          <Navbar.Brand >Hello</Navbar.Brand>
          <Navbar.Toggle aria-controls="basic-navbar-nav" />
          <Navbar.Collapse id="basic-navbar-nav">
            <Nav className="ml-auto">
              <Nav.Link as={Link} to='/'>Login</Nav.Link>
              <Nav.Link as={Link} to='/register'>Register</Nav.Link>
            </Nav>
          </Navbar.Collapse>
        </Navbar>
    )
  }
}

export default withRouter(NavigationBar);
4

0 回答 0