注销重定向不正确重定向

时间:2020-12-21 20:46:43

标签: react-admin

我在 react-admin 中有一个自定义的 AuthProvider 我正在使用 checkAuth 函数并且逻辑似乎运行正确:

checkAuth: () => {
    if(localStorage.getItem("userprofile")) {
      return Promise.resolve()
    } else {
      return Promise.reject({ redirectTo: '/login', message: 'login.required' })
    }
  }

问题是生成的路由 url 似乎默认为特定资源。 redirectTo调用后浏览器重定向到:http://localhost:3001/login#/clients 而不是预期的 http://localhost:3001/login

是否有一些逻辑原因的默认路由设置?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我使用 react-admin 3.8.5,为了改变这种行为,我不得不使用 customSagas: https://marmelab.com/react-admin/Admin.html#customsagas

logoutSaga.js

import { UNREGISTER_RESOURCE } from 'react-admin'
import { replace } from 'connected-react-router'
import { put, takeEvery } from 'redux-saga/effects'

function* logoutMonitor(action) {
  try {
    if (action.payload === 'your resource name') {
      yield put(replace({pathname: '/login', state: {nextPathname: '/'}})) // Changing the route for the next entrance!
    }
  } catch (error) {
    console.warn('logoutSaga:', error)
  }
}

function* logoutSaga() {
  yield takeEvery([UNREGISTER_RESOURCE], logoutMonitor)
}

export default logoutSaga