import { routerRedux } from 'dva/router'; import { fakeAccountLogin } from '../services/api'; import { setAuthority } from '../utils/authority'; import { reloadAuthorized } from '../utils/Authorized'; export default { namespace: 'login', state: { status: undefined, }, effects: { *login({ payload }, { call, put }) { const response = yield call(fakeAccountLogin, payload); yield put({ type: 'changeLoginStatus', payload: response, }); // Login successfully if (response.status === 'ok') { reloadAuthorized(); const urlParams = new URL(window.location.href); let redirect = urlParams.searchParams.get('redirect'); if (redirect) { const redirectUrlParams = new URL(redirect); if (redirectUrlParams.origin === urlParams.origin) { redirect = redirect.substr(urlParams.origin.length); } else { window.location.href = redirect; return; } } yield put(routerRedux.push(redirect || '/')); } }, *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); window.history.replaceState(null, 'login', urlParams.href); } finally { yield put({ type: 'changeLoginStatus', payload: { status: false, currentAuthority: 'guest', }, }); reloadAuthorized(); yield put(routerRedux.push('/user/login')); } }, }, reducers: { changeLoginStatus(state, { payload }) { setAuthority(payload.currentAuthority); return { ...state, status: payload.status, type: payload.type, }; }, }, };