login.js 1.6 KB
Newer Older
afc163's avatar
afc163 committed
1
import { fakeAccountLogin } from '../services/api';
ddcat1115's avatar
ddcat1115 committed
2
import { setAuthority } from '../utils/authority';
3 4 5 6 7 8 9 10 11

export default {
  namespace: 'login',

  state: {
    status: undefined,
  },

  effects: {
afc163's avatar
afc163 committed
12
    *login({ payload }, { call, put }) {
13 14 15 16
      yield put({
        type: 'changeSubmitting',
        payload: true,
      });
ddcat1115's avatar
ddcat1115 committed
17
      const response = yield call(fakeAccountLogin, payload);
18
      yield put({
19
        type: 'changeLoginStatus',
20 21
        payload: response,
      });
afc163's avatar
afc163 committed
22 23
      // Login successfully
      if (response.status === 'ok') {
ddcat1115's avatar
ddcat1115 committed
24 25 26 27 28
        // 非常粗暴的跳转,登陆成功之后权限会变成user或admin,会自动重定向到主页
        // Login success after permission changes to admin or user
        // The refresh will automatically redirect to the home page
        // yield put(routerRedux.push('/'));
        location.reload();
afc163's avatar
afc163 committed
29
      }
30
    },
31
    *logout(_, { put }) {
ddcat1115's avatar
fix #52  
ddcat1115 committed
32
      yield put({
33 34 35
        type: 'changeLoginStatus',
        payload: {
          status: false,
ddcat1115's avatar
ddcat1115 committed
36
          currentAuthority: 'guest',
37
        },
ddcat1115's avatar
fix #52  
ddcat1115 committed
38
      });
ddcat1115's avatar
ddcat1115 committed
39 40 41 42
      // yield put(routerRedux.push('/user/login'));
      // Login out after permission changes to admin or user
      // The refresh will automatically redirect to the login page
      location.reload();
ddcat1115's avatar
fix #52  
ddcat1115 committed
43
    },
44 45 46
  },

  reducers: {
47
    changeLoginStatus(state, { payload }) {
ddcat1115's avatar
ddcat1115 committed
48
      setAuthority(payload.currentAuthority);
49 50 51 52
      return {
        ...state,
        status: payload.status,
        type: payload.type,
afc163's avatar
afc163 committed
53
        submitting: false,
54 55 56 57 58 59 60 61 62 63
      };
    },
    changeSubmitting(state, { payload }) {
      return {
        ...state,
        submitting: payload,
      };
    },
  },
};