在组件安装之前进行授权检查的最佳做法是什么?React路由器授权
我用反应路由器的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>
);
}
});
https://github.com/rackt/react-router/tree/master/examples/auth-flow你怎么虽然检查?从一个cookie?从服务器调用?我认为它通常是在'Route'的'onEnter'中完成的,而不是'componentWillMount'。 onEnter = {function(nextState,transition){if(!USER_IS_AUTHED){transition.to('login'); }})}' – Dylan