I use react-router and react-router-redux, I will redirect user to Login page if he tries to visit a page requiring logged in without logging in:
<Route path="/react/myprofile" component={MyProfile} onEnter={requireAuth} /></Route>
function requireAuth(nextState, replace) {
const state_user = store.getState().auth.user;
if (!state_user) {
console.log('not logged in, redirect')
replace({
pathname: '/react/login',
state: { nextPathname: nextState.location.pathname }
})
}
}
And after the user logs in, I will redirect the user to "/react/myprofile": in my redux-saga, I do the following:
const route_state = get routing state from the store;
redirectTo(route_state.locationBeforeTransitions.state.nextPathname);
As above, I have to retrieve the information from route_state.locationBeforeTransitions.state.nextPathname
, it is too verbose, and it seems strange to do as this.
I wonder if there is a better way to do this? Thanks.