【发布时间】: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 你是怎么检查的?从饼干?从服务器调用?我认为它通常在
Route的onEnter中完成,而不是componentWillMount。<Route path='/' component={Dashboard} onEnter={function(nextState, transition) { if (!USER_IS_AUTHED) { transition.to('login'); }})}
标签: javascript reactjs authorization react-router