【发布时间】:2016-10-23 20:37:43
【问题描述】:
我创建了一个中间件,用于检查请求是否返回无效的访问响应。如果状态是401,我想将用户重定向到登录页面
这是中间件代码
import React from 'react';
import { push, replace } from 'react-router-redux';
const auth_check = ({ getState }) => {
return (next) => (action) => {
if(action.payload != undefined && action.payload.status==401){
push('login');
console.log('session expired');
}
// Call the next dispatch method in the middleware chain.
let returnValue = next(action);
return returnValue
}
}
export default auth_check;
将其包含在 index.js 中
...
const store = createStore(reducers, undefined,
compose(
applyMiddleware(promise,auth_check)
)
);
const history = syncHistoryWithStore(browserHistory, store);
ReactDOM.render(
<Provider store={store}>
<Router history={history} routes={routes} />
</Provider>
, document.querySelector('.app'));
push 方法不会重定向页面。我确信代码会通过该部分,因为日志正在显示
【问题讨论】:
-
看看下面我的回答,希望对你有所帮助。
标签: reactjs react-router react-jsx react-redux