在 redux 中间件中使用 react-router 重定向
Redirect using react-router in redux middleware
我创建了一个中间件来检查请求是否 returns 是无效的访问响应。如果状态是 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'));
推送方法不重定向页面。我确信代码通过了该部分,因为日志显示
使用onEnter
react-router 方法。调用将检查访问权限的函数。示例:
function checkAccess() {
//some logic to check
if(notAuthorized){ window.location.href= "/login"; }
}
如果您更喜欢 Redux 风格的动作,该库还提供了一组动作创建器和一个 中间件来捕获它们并将它们重定向到您的历史实例。
为:push(location), replace(location), go(number), goBack(), goForward()
您必须安装 routerMiddleware 才能使这些动作创建器工作。
import { routerMiddleware, push } from 'react-router-redux'
// Apply the middleware to the store
const middleware = routerMiddleware(browserHistory)
const store = createStore(
reducers,
applyMiddleware(middleware)
)
// Dispatch from anywhere like normal.
store.dispatch(push('/foo'))
React Router 还提供单例版本的历史记录(browserHistory 和 hashHistory),您可以在应用程序的任何地方导入和使用它们。
import { browserHistory } from 'react-router'
if(action.payload != undefined && action.payload.status==401){
browserHistory.push('login');
console.log('session expired');
}
顺便说一下,您可以使用 onEnter or redux-auth-wrapper
检查身份验证
我创建了一个中间件来检查请求是否 returns 是无效的访问响应。如果状态是 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'));
推送方法不重定向页面。我确信代码通过了该部分,因为日志显示
使用onEnter
react-router 方法。调用将检查访问权限的函数。示例:
function checkAccess() {
//some logic to check
if(notAuthorized){ window.location.href= "/login"; }
}
如果您更喜欢 Redux 风格的动作,该库还提供了一组动作创建器和一个 中间件来捕获它们并将它们重定向到您的历史实例。
为:push(location), replace(location), go(number), goBack(), goForward()
您必须安装 routerMiddleware 才能使这些动作创建器工作。
import { routerMiddleware, push } from 'react-router-redux'
// Apply the middleware to the store
const middleware = routerMiddleware(browserHistory)
const store = createStore(
reducers,
applyMiddleware(middleware)
)
// Dispatch from anywhere like normal.
store.dispatch(push('/foo'))
React Router 还提供单例版本的历史记录(browserHistory 和 hashHistory),您可以在应用程序的任何地方导入和使用它们。
import { browserHistory } from 'react-router'
if(action.payload != undefined && action.payload.status==401){
browserHistory.push('login');
console.log('session expired');
}
顺便说一下,您可以使用 onEnter or redux-auth-wrapper
检查身份验证