Answer the question
In order to leave comments, you need to log in
Why doesn't react-router onEnter fire on the parent component after a redirect?
Guys, hello everyone. Rescue, please.
There is a standard react-redux application that works using web sockets for 2-way data flow. Authorization is implemented on cookie sessions.
The standard implementation of routing is something like this:
<Provider store={store}>
<Router history={browserHistory}>
<Route path="/" component={App} onEnter={App.onEnter}>
<IndexRedirect to="/list" />
<Route path="list" component={List} >
<Route path=":id" component={ListElement} onEnter={ListElement.onEnter}/>
</Route>
<Route path="/login" component={LoginPage} />
</Route>
</Router>
</Provider>
static onEnter(nextState, replace, callback) {
const { getState, dispatch } = store;
const { auth, user } = getState();
const { pathname } = nextState.location;
if (!auth.isAuthenticated && !auth.error || auth.isAuthenticated && !user.id) {
dispatch(checkUserAuth())
.then(user => { // если юзер в системы и его сессия найдена, то сервер отдают данные юзера
dispatch(signinUser()); // action-creator, который меняет state.auth.isAuthenticated = true
dispatch(updateUser(user.data)); // просто заполняет данными объекты юзера(использую axios)
replace(pathname); // далее отправляет клиента туда, куда он хочет. Главное - проверить его сессию.
callback();
})
.catch(error => {
dispatch(authenticateUser(error.message)); // обрабатываем ошибки
replace('/login'); // отправляет обратно на /login, если юзер не имеет сессии
callback(error);
});
} else {
callback();
}
}
Answer the question
In order to leave comments, you need to log in
try to change
on
a .onEnter() call in the container's componentDidMount()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question