Giter Club home page Giter Club logo

Comments (1)

onreadystatechange avatar onreadystatechange commented on June 16, 2024
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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.