Comments (1)
import {push} from "react-router-redux";
import {unAuth, LOGIN_FAIL} from "./modules/userAuth";
const defaultTypeSuffixes = ['SUCCESS', 'FAIL']
export default function authMiddleware(config = {}) {
const promiseTypeSuffixes = config.promiseTypeSuffixes || defaultTypeSuffixes
return ({dispatch, getState}) => next => action => {
if (action.type) {
//定义失败与成功的变量值
const [SUCCESS, FAIL] = promiseTypeSuffixes
//正则判断
const isSuccess = new RegExp(`${SUCCESS}$`, 'g')
const isFail = new RegExp(`${FAIL}$`, 'g')
var unLogined = false;
if (action.type.match(isSuccess)) {
//判断状态码
if (action.data && action.data.code == -2) {
unLogined = true;
}
} else if (action.type.match(isFail)) {
//判断状态码
if (action.error && action.error.code && action.error.code == -2) {
unLogined = true;
}
}
//登录失效切action type不为登录失败状态 执行路由自动跳转
if (unLogined && action.type != LOGIN_FAIL) {
const state = getState();
const url = state && state.routing && state.routing.locationBeforeTransitions && state.routing.locationBeforeTransitions.pathname;
dispatch(unAuth());
//将用户前一次访问地址编码并拼接在url中,再次登录成功后跳转至原网页
dispatch(push("/login" + ((url && url != '/login') ? "?url=" + encodeURIComponent(url) : "")));
}
}
//执行下一个中间件
return next(action)
}
}
from blogs.
Related Issues (5)
- 公用fetch封装实现,可在fetch中判断状态码,执行副作用 HOT 2
- 编辑器复制粘贴核心代码 HOT 2
- 贴一个webpack配置,基于webpack2.x HOT 1
- 笔记导出Word 核心代码(也可以是任何一段dom导出成word) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blogs.