我与redux一起使用react并使用JWT令牌来认证用户,JWT的寿命很短,我编写了一个中间件来刷新令牌,如下所示:
PUT https://management.azure.com/subscriptions/{subscription-id}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM?api-version=2019-03-01
但是现在我想编写一个没有redux并从import { refreshToken, refreshTokenFail } from '../actions/index';
const jwtTokenRefresh = ({ dispatch, getState }) => next => action => {
if (typeof action === 'function') {
const timeStamp = Math.floor(Date.now() / 1000);
const expiration = getState().auth.expiration;
if (expiration && timeStamp > expiration && !getState().auth.refreshToken) {
return refreshToken(dispatch)
.then(() => {
return next(action)
}).catch(() => {
console.log('fail')
dispatch(refreshTokenFail(dispatch)).then(() =>{
window.location.href= "/login";
})
});
}
}
return next(action);
};
export default jwtTokenRefresh;
开始使用的新React应用,如何在react上下文中刷新令牌?