login.js 1.6 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import { routerRedux } from 'dva/router';
2
import { fakeAccountLogin, getFakeCaptcha } from '../services/api';
ddcat1115's avatar
ddcat1115 committed
3
import { setAuthority } from '../utils/authority';
陈帅's avatar
陈帅 committed
4
import { reloadAuthorized } from '../utils/Authorized';
5 6 7 8 9 10 11 12 13

export default {
  namespace: 'login',

  state: {
    status: undefined,
  },

  effects: {
afc163's avatar
afc163 committed
14
    *login({ payload }, { call, put }) {
ddcat1115's avatar
ddcat1115 committed
15
      const response = yield call(fakeAccountLogin, payload);
16
      yield put({
17
        type: 'changeLoginStatus',
18 19
        payload: response,
      });
afc163's avatar
afc163 committed
20 21
      // Login successfully
      if (response.status === 'ok') {
陈帅's avatar
陈帅 committed
22 23
        reloadAuthorized();
        yield put(routerRedux.push('/'));
afc163's avatar
afc163 committed
24
      }
25
    },
26 27 28 29 30 31 32
    *logout(_, { put, select }) {
      try {
        // get location pathname
        const urlParams = new URL(window.location.href);
        const pathname = yield select(state => state.routing.location.pathname);
        // add the parameters in the url
        urlParams.searchParams.set('redirect', pathname);
jim's avatar
jim committed
33
        window.history.replaceState(null, 'login', urlParams.href);
34 35 36 37 38 39 40 41
      } finally {
        yield put({
          type: 'changeLoginStatus',
          payload: {
            status: false,
            currentAuthority: 'guest',
          },
        });
陈帅's avatar
陈帅 committed
42 43
        reloadAuthorized();
        yield put(routerRedux.push('/user/login'));
44
      }
ddcat1115's avatar
fix #52  
ddcat1115 committed
45
    },
46 47 48
    *getCaptcha({ payload }, { call }) {
      yield call(getFakeCaptcha, payload);
    },
49 50 51
  },

  reducers: {
52
    changeLoginStatus(state, { payload }) {
ddcat1115's avatar
ddcat1115 committed
53
      setAuthority(payload.currentAuthority);
54 55 56 57 58 59 60 61
      return {
        ...state,
        status: payload.status,
        type: payload.type,
      };
    },
  },
};