【问题标题】:React Router Authorization反应路由器授权
【发布时间】:2015-12-30 03:19:14
【问题描述】:

在安装组件之前进行授权检查的最佳做法是什么?

我使用 react-router 1.x

这是我的路线

React.render((
  <Router history={History.createHistory()}>
    <Route path="/" component={Dashboard}></Route>
    <Route path="/login" component={LoginForm}></Route>
  </Router>
), document.body);

这是我的仪表板组件:

var Dashboard = React.createClass({
  componentWillMount: function () {
    // I want to check authorization here
    // If the user is not authorized they should be redirected to the login page.
    // What is the right way to perform this check?
  },

  render: function () {
    return (
      <h1>Welcome</h1>
    );
  }
});

【问题讨论】:

  • github.com/rackt/react-router/tree/master/examples/auth-flow 你是怎么检查的?从饼干?从服务器调用?我认为它通常在RouteonEnter 中完成,而不是componentWillMount&lt;Route path='/' component={Dashboard} onEnter={function(nextState, transition) { if (!USER_IS_AUTHED) { transition.to('login'); }})}

标签: javascript reactjs authorization react-router


【解决方案1】:

更新的解决方案 React 路由器 v4

<Route 
  path="/some-path" 
  render={() => !isAuthenticated ?
    <Login/> :
    <Redirect to="/some-path" />
}/>

React 路由器最高 v3

使用'onEnter'事件并在回调中检查用户是否被授权:

<Route path="/" component={App} onEnter={someAuthCheck}>  

const someAuthCheck = (nextState, transition) => { ... }

【讨论】:

  • 在示例和文档方面情况变得更糟了。 “auth-flow”示例对我不起作用,并且很难找到有关处理程序的第二个参数应该接受什么的信息,因此我可以尝试不同的事情。
  • onEnter(nextState, replace, callback?) "在即将进入路由时调用。它提供下一个路由器状态和一个函数重定向到另一个路径。这将是触发钩子的路由实例。”
  • 没有。不是一个好的解决方案。通常,您会在您所在的州保留有关授权的信息。像这样,例如:'isAuth:true'。你在你的组件中有这个变量作为道具,你只需将它自己传递到你需要它的地方。但是您不能将任何状态变量传递给“Route”。因此,您不得不从“路由”向您的服务器发出“获取”调用、Ajax 请求,以确定用户是否已登录。使用简单的状态变量并发出具有相同目的的 Ajax 请求是无稽之谈和糟糕的设计.
  • @Green 这里可能有些混淆:你是说你应该使用来自该州的信息。但是在onEnter 函数中,您可以简单地访问您的简单状态变量(状态是函数的第一个参数),不需要额外的 ajax 请求
  • 对于来自 Google 搜索的用户:onEnter 不再存在于 react-router-4。见:stackoverflow.com/questions/42768620/…
【解决方案2】:

使用 react-router 4,您可以访问组件内的 Route props。要重定向用户,您只需将新 URL 推送到历史记录。在您的示例中,代码为:

var Dashboard = React.createClass({
  componentWillMount: function () {
    const history = this.props.history; // you'll have this available
    // You have your user information, probably from the state
    // We let the user in only if the role is 'admin'
    if (user.role !== 'admin') {
      history.push('/'); // redirects the user to '/'
    }
  },

  render: function () {
    return (
      <h1>Welcome</h1>
    );
  }
});

在文档中,他们通过使用render 属性而不是component 来显示another way to do it。他们定义了一个PrivateRoute,当您定义所有路由时,这使得代码非常明确。

【讨论】:

    【解决方案3】:

    如果你想在多个组件上应用授权,那么你可以这样做。

    <Route onEnter={requireAuth} component={Header}>
        <Route path='dashboard' component={Dashboard} />
        <Route path='events' component={Events} />
    </Route>
    

    对于单个组件,您可以这样做

    <Route onEnter={requireAuth} component={Header}/>
    
    function requireAuth(nextState, replaceState) {
      if (token || or your any condition to pass login test)
      replaceState({ nextPathname: nextState.location.pathname }, 
      '/login')
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 2019-02-18
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      相关资源
      最近更新 更多