erikras / react-redux-universal-hot-example

A starter boilerplate for a universal webapp using express, react, redux, webpack, and react-transform
MIT License
12.01k stars 2.5k forks source link

Store and state lost in next route on replace() inside onEnter #1294

Open aseem2625 opened 7 years ago

aseem2625 commented 7 years ago

So, as in the boilerplate..

const requireLogin = (nextState, replace, cb) => {

    if(!isLoggedIn) {
        store.dispatch(getUserInfo(somePayLoad)).then(checkLoginStatus);
    }
    function checkLoginStatus() {
        const {authorization: { user }} = store.getState();
        if(!user.isSpecial && nextState.location.pathname === '/special') {
            replace('/home');
        }
        cb();
    }
}

So, when I replace(), and if the if condition !user.isSpecial fails, then it goes to the usual called route, access the user details that were fetched as in above code and also, I can easily call logout dispatcher I wrote whenever user wants to exit. But when if condition is passed, it replaces to /home page and opens(redirects) to home page successfully. But I can no longer have user details in authorization store. Neither I can call logout dispatcher.

How to achieve this?

VSuryaBhargava commented 7 years ago

You may need an else after if (! isLoggedIn).

Other than that

You may be dispatching some actions which alter the authentication state in home page.

aseem2625 commented 7 years ago

Hi @VSuryaBhargava In my case, if a user is logged in then user Details will be available in every case. And I think, that it's not just in my case but in mostly other cases also.

Also, authentication state in homepage has nothing to do with authentication to particular route. I don't want a person to keep logged in, send him to homepage and he can access other pages. Only that particular route he can't access!