React Router + Axios 拦截器。如何进行重定向?
React Router + Axios interceptors. How to Do a redirect?
我有一个 axios 拦截器,当用户被强制注销(因为令牌过期)时,我想返回我的主页。
虽然我不确定如何将 React 路由器传递给它。我正在使用 mobx,但不确定是否能帮助我解决这个问题。
export const axiosInstance = axios.create({
baseURL: 'https://localhost:44391/api',
timeout: 5000,
contentType: "application/json",
Authorization: getAuthToken()
})
axiosInstance.interceptors.response.use(function (response) {
return response;
}, function (error) {
const originalRequest = error.config;
if(error.code != "ECONNABORTED" && error.response.status === 401 && !originalRequest._retry){
originalRequest._retry = true;
return axiosInstance.post("/tokens/auth",{
"refreshToken": getRefreshToken(),
"grantType": "refresh_token"
}).then(response => {
localStorage.authentication = JSON.stringify(response.data);
updateAuthInstant();
return axiosInstance(originalRequest)
});
}
return Promise.reject(error);
});
您应该添加 history npm 包。有了它,您可以创建浏览器历史记录并在应用程序的其他地方使用它。
例如:
// routing.js
import createHistory from 'history/createBrowserHistory';
export const history = createHistory();
在包含路由的组件中。
import { Route, Router } from 'react-router-dom';
import { history } from './routing.js';
import ComponentA from './ComponentA';
import ComponentB from './ComponentB';
const Routes = () => (
<Router history={history}>
<div>
<Route exact path='/route1' component={ComponentA} />
<Route exact path='/route2' component={ComponentB} />
</div>
</Router>
);
并且在您要控制路由的其他文件中:
import { history } from './routing.js';
function changeRoute() {
// things happening here..
history.push('/route2');
}
调用 changeRoute()
时,路径更新为 /route2
并且将呈现 ComponentB
。
window.location.href = '/user/login'
我有一个 axios 拦截器,当用户被强制注销(因为令牌过期)时,我想返回我的主页。
虽然我不确定如何将 React 路由器传递给它。我正在使用 mobx,但不确定是否能帮助我解决这个问题。
export const axiosInstance = axios.create({
baseURL: 'https://localhost:44391/api',
timeout: 5000,
contentType: "application/json",
Authorization: getAuthToken()
})
axiosInstance.interceptors.response.use(function (response) {
return response;
}, function (error) {
const originalRequest = error.config;
if(error.code != "ECONNABORTED" && error.response.status === 401 && !originalRequest._retry){
originalRequest._retry = true;
return axiosInstance.post("/tokens/auth",{
"refreshToken": getRefreshToken(),
"grantType": "refresh_token"
}).then(response => {
localStorage.authentication = JSON.stringify(response.data);
updateAuthInstant();
return axiosInstance(originalRequest)
});
}
return Promise.reject(error);
});
您应该添加 history npm 包。有了它,您可以创建浏览器历史记录并在应用程序的其他地方使用它。
例如:
// routing.js
import createHistory from 'history/createBrowserHistory';
export const history = createHistory();
在包含路由的组件中。
import { Route, Router } from 'react-router-dom';
import { history } from './routing.js';
import ComponentA from './ComponentA';
import ComponentB from './ComponentB';
const Routes = () => (
<Router history={history}>
<div>
<Route exact path='/route1' component={ComponentA} />
<Route exact path='/route2' component={ComponentB} />
</div>
</Router>
);
并且在您要控制路由的其他文件中:
import { history } from './routing.js';
function changeRoute() {
// things happening here..
history.push('/route2');
}
调用 changeRoute()
时,路径更新为 /route2
并且将呈现 ComponentB
。
window.location.href = '/user/login'