使用react-router-v4在路由上执行身份验证

Riy*_*ria 5 authentication redirect reactjs react-router

我正在写Authentication我的支票DashBoard.但是函数本身并没有被调用.谁能给我一些解决方案呢?我在ReactJs开发.

这是路线部分:

 <Router>
            <div>
              <Route exact path={"/"} component={Home} />
              <Route path={"/SignUp"} component={SignUp} />
              <Route path={"/SignIn"} component={SignIn} />
              <Route path={"/Dashboard"} component={Dashboard} onEnter={this.requireAuth} />
            </div>
          </Router>
Run Code Online (Sandbox Code Playgroud)

这是功能:

  requireAuth (nextState, replace) {
    console.log("?????????????",this.state.getToken);
    if(!this.state.getToken) {
      replace({pathname: '/'});
    }
  }
Run Code Online (Sandbox Code Playgroud)

Shu*_*tri 13

react-router v4,你可以利用render propRoute与生命周期方法一起更换onEnter功能存在于反应路由器V3.

有关详细信息,请参阅此答案:

onEtern prop in react-router v4

但是,由于您只想在onEnter prop中进行身份验证,因此您可以轻松创建一个执行此操作的HOC

const RequireAuth = (Component) => { 

    return class App extends Component { 

        componentWillMount() { 
            const getToken = localStorage.getItem('token'); 
            if(!getToken) { 
               this.props.history.replace({pathname: '/'}); 
            } 
        } 
        render() { 
           return <Component {...this.props} /> 
        }
    } 

} 

export { RequireAuth }
Run Code Online (Sandbox Code Playgroud)

并使用它

<Route path={"/Dashboard"} component={RequireAuth(Dashboard)}/>
Run Code Online (Sandbox Code Playgroud)

编辑:如果您需要进行网络调用以查找是否使用了身份验证,您可以编写HOC之类的

 const RequireAuth = (Component) => { 

    return class App extends Component { 
        state = {
            isAuthenticated: false,
            isLoading: true
        }

        componentDidMount() {
            AuthCall().then(() => {
                this.setState({isAuthenticated: true, isLoading: false});
            }).catch(() => {
                this.setState({isLoading: false});
            })
        } 
        render() { 
           const { isAuthenticated, isLoading } = this.state;
           if(isLoading) {
               return <div>Loading...</div>
           }
           if(!isAuthenticated) {
               return <Redirect to="/login" />
           }
           return <Component {...this.props} /> 
        }
    } 

} 

export { RequireAuth }
Run Code Online (Sandbox Code Playgroud)

更新:

除了HOC之外,您还可以选择PrivateRoute类似的组件

const PrivateRoute = ({component: Component, isAuthenticated, isLoading, ...rest }) => { 
           if(isLoading) {
               return <div>Loading...</div>
           }
           if(!isAuthenticated) {
               return <Redirect to="/login" />
           }
           return <Component {...this.props} /> 
        }
    } 
} 

 export { PrivateRoute };
Run Code Online (Sandbox Code Playgroud)

你可以像使用它一样

  class App extends Component { 
        state = {
            isAuthenticated: false,
            isLoading: true
        }

        componentDidMount() {
            AuthCall().then(() => {
                this.setState({isAuthenticated: true, isLoading: false});
            }).catch(() => {
                this.setState({isLoading: false});
            })
        } 
        render() { 
           <Router>
              <div>
                  <Route exact path={"/"} component={Home} />
                  <Route path={"/SignUp"} component={SignUp} />
                  <Route path={"/SignIn"} component={SignIn} />
                  <PrivateRoute path={"/Dashboard"} component={Dashboard} isAuthenticated={this.state.isAuthenticated} isLoading={this.isLoading}/>
               </div>
           </Router>
        }
    } 
Run Code Online (Sandbox Code Playgroud)

  • 有人可以不只是通过 React 开发工具编辑 React 状态来使“isAuthenticated: true”和“isLoading: false”吗?然后他们就可以访问受保护的路线 (3认同)