nwood2009
nwood2009

Reputation: 75

Logout redirectTo not redirecting correctly

I have a custom AuthProvider in react-admin I am using the checkAuth function and the logic seems to be running correctly:

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

The issue is that the resulting routed url seems to be defaulting to a specific resource. After the redirectTo call the browser redirects to: http://localhost:3001/login#/clients instead of the expected http://localhost:3001/login

Is there some default route setting of some logic reason for this?

Thanks for you help.

Upvotes: 1

Views: 221

Answers (1)

MaxAlex
MaxAlex

Reputation: 3319

I use react-admin 3.8.5 and to change this behavior I had to use 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

Upvotes: 1

Related Questions