Redux persist rehydrate previous auth state too lateRedux middleware for dynamic auth / redirectingReactjs, Redux - redux-persistHow to stop redux-form's “form” state from auto-rehydrated by redux-persitHow to keep redux state when page refreshedRedux persist v5 not working as it is supposed toredux-persist persisting my logged in stateReact, persisting redux state or reauthenticating via jwt to keep a user logged in?protected routes issue switching from react-router-redux to connected-react-routerPage reload causes redux state to turn DEFAULT (REACT/REDUX)PersistGate in redux-persist doesn't wait the store to be rehydrated

Have 1.5% of all nuclear reactors ever built melted down?

How should I introduce map drawing to my players?

Question in discrete mathematics about group permutations

Why isn't 'chemically-strengthened glass' made with potassium carbonate to begin with?

Of strange atmospheres - the survivable but unbreathable

Why aren't space telescopes put in GEO?

Compaq Portable vs IBM 5155 Portable PC

Best material to absorb as much light as possible

Apt - strange requests to d16r8ew072anqo.cloudfront.net:80

Value of a binomial series

Is the Indo-European language family made up?

First Match - awk

USPS Back Room - Trespassing?

How to ignore kerning of underbrace in math mode

Why did Jon Snow do this immoral act if he is so honorable?

What is a Power on Reset IC?

Is it truly impossible to tell what a CPU is doing?

Can a person survive on blood in place of water?

Pirate democracy at its finest

In general, would I need to season a meat when making a sauce?

Need to read my home electrical meter

Is "cool" appropriate or offensive to use in IMs?

Can I connect my older mathematica front-end to the free wolfram engine?

How can I tell if I'm being too picky as a referee?



Redux persist rehydrate previous auth state too late


Redux middleware for dynamic auth / redirectingReactjs, Redux - redux-persistHow to stop redux-form's “form” state from auto-rehydrated by redux-persitHow to keep redux state when page refreshedRedux persist v5 not working as it is supposed toredux-persist persisting my logged in stateReact, persisting redux state or reauthenticating via jwt to keep a user logged in?protected routes issue switching from react-router-redux to connected-react-routerPage reload causes redux state to turn DEFAULT (REACT/REDUX)PersistGate in redux-persist doesn't wait the store to be rehydrated






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








8















I have setup routes that are meant to be authenticated to redirect user to login page if unauthenticated. I have also setup redux-persist to auto dehydrate my auth state so user remains login on refresh. The problem is this rehydration is too late and user is already redirected to login page



enter image description here



The 1st location change is to an authenticated route, the 2nd is to login. Notice rehydrate comes after these. Ideally it should be right after @@INIT at least?










share|improve this question

















  • 1





    You can solve it by two different approaches, 1. Create a page processing/loading and when you receive user authentication status redirect to page you need. 2. The 1st location should be login and if already logged then redirect. Better ux

    – hod caspi
    Apr 4 '17 at 18:34

















8















I have setup routes that are meant to be authenticated to redirect user to login page if unauthenticated. I have also setup redux-persist to auto dehydrate my auth state so user remains login on refresh. The problem is this rehydration is too late and user is already redirected to login page



enter image description here



The 1st location change is to an authenticated route, the 2nd is to login. Notice rehydrate comes after these. Ideally it should be right after @@INIT at least?










share|improve this question

















  • 1





    You can solve it by two different approaches, 1. Create a page processing/loading and when you receive user authentication status redirect to page you need. 2. The 1st location should be login and if already logged then redirect. Better ux

    – hod caspi
    Apr 4 '17 at 18:34













8












8








8


3






I have setup routes that are meant to be authenticated to redirect user to login page if unauthenticated. I have also setup redux-persist to auto dehydrate my auth state so user remains login on refresh. The problem is this rehydration is too late and user is already redirected to login page



enter image description here



The 1st location change is to an authenticated route, the 2nd is to login. Notice rehydrate comes after these. Ideally it should be right after @@INIT at least?










share|improve this question














I have setup routes that are meant to be authenticated to redirect user to login page if unauthenticated. I have also setup redux-persist to auto dehydrate my auth state so user remains login on refresh. The problem is this rehydration is too late and user is already redirected to login page



enter image description here



The 1st location change is to an authenticated route, the 2nd is to login. Notice rehydrate comes after these. Ideally it should be right after @@INIT at least?







reactjs redux react-redux






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 2 '17 at 13:18









Jiew MengJiew Meng

20.7k142382653




20.7k142382653







  • 1





    You can solve it by two different approaches, 1. Create a page processing/loading and when you receive user authentication status redirect to page you need. 2. The 1st location should be login and if already logged then redirect. Better ux

    – hod caspi
    Apr 4 '17 at 18:34












  • 1





    You can solve it by two different approaches, 1. Create a page processing/loading and when you receive user authentication status redirect to page you need. 2. The 1st location should be login and if already logged then redirect. Better ux

    – hod caspi
    Apr 4 '17 at 18:34







1




1





You can solve it by two different approaches, 1. Create a page processing/loading and when you receive user authentication status redirect to page you need. 2. The 1st location should be login and if already logged then redirect. Better ux

– hod caspi
Apr 4 '17 at 18:34





You can solve it by two different approaches, 1. Create a page processing/loading and when you receive user authentication status redirect to page you need. 2. The 1st location should be login and if already logged then redirect. Better ux

– hod caspi
Apr 4 '17 at 18:34












2 Answers
2






active

oldest

votes


















13





+400









The persistStore function which is used to make your store persistent have a third param callback which is invoked after store rehydration is done. You have to start your app with some sort of preloader, which waits for rehydration to happen and renders your full application only after it finishes.



redux-persist docs even have a recipe for this scenario. In your case all the react-router stuff should be rendered inside initial loader as well:



export default class Preloader extends Component 

constructor()
super()
this.state = rehydrated: false


componentWillMount()
persistStore(this.props.store, , () =>
this.setState( rehydrated: true );
)


render()
if(!this.state.rehydrated)
return <Loader />;

return (
<Provider store=store>
<ConnectedRouter history=history>
<App />
</ConnectedRouter>
</Provider>
);



const store = ...; // creating the store but not calling persistStore yet
ReactDOM.render(<Preloader store=store />, ... );





share|improve this answer























  • There is also the PersistGate component now that makes delaying rendering until rehydrated even easier and more logical: github.com/rt2zz/redux-persist

    – SomethingOn
    Aug 22 '18 at 15:36











  • @SomethingOn doesn't help it seems. Have the same issue with PersistGate even.

    – Petros Kyriakou
    Feb 28 at 6:49











  • @PetrosKyriakou sounds like you may need to add some checks in to determine when/what to render. The PersistGate shouldn't render any child components until the store has been rehydrated. Set a breakpoint in the render method and verify that it's not being called until the rehydrate action has fired. If it's rendering after the store has been rehydrated, verify your containers/selectors, etc are setting the props appropriately for your component.

    – SomethingOn
    Mar 4 at 15:16


















0














When user refresh the page, The first LOCATION_CHANGE will fire because you sync history with store. However, it only syncs the history and does not redirect user to anywhere yet.



The only thing matters is the second LOCATION_CHANGE, which should occur after persistStore(). Good news is persistStore() has a call back.



const store = ...

// persist store
persistStore(store, options, () =>
const states = store.getState();

// if authenticated, do nothing,
// if not, redirect to login page
if (!states.auth.isAuthenticated)
// redirect to login page

);

render(...);


Hope this can help to solve your problem.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43168801%2fredux-persist-rehydrate-previous-auth-state-too-late%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    13





    +400









    The persistStore function which is used to make your store persistent have a third param callback which is invoked after store rehydration is done. You have to start your app with some sort of preloader, which waits for rehydration to happen and renders your full application only after it finishes.



    redux-persist docs even have a recipe for this scenario. In your case all the react-router stuff should be rendered inside initial loader as well:



    export default class Preloader extends Component 

    constructor()
    super()
    this.state = rehydrated: false


    componentWillMount()
    persistStore(this.props.store, , () =>
    this.setState( rehydrated: true );
    )


    render()
    if(!this.state.rehydrated)
    return <Loader />;

    return (
    <Provider store=store>
    <ConnectedRouter history=history>
    <App />
    </ConnectedRouter>
    </Provider>
    );



    const store = ...; // creating the store but not calling persistStore yet
    ReactDOM.render(<Preloader store=store />, ... );





    share|improve this answer























    • There is also the PersistGate component now that makes delaying rendering until rehydrated even easier and more logical: github.com/rt2zz/redux-persist

      – SomethingOn
      Aug 22 '18 at 15:36











    • @SomethingOn doesn't help it seems. Have the same issue with PersistGate even.

      – Petros Kyriakou
      Feb 28 at 6:49











    • @PetrosKyriakou sounds like you may need to add some checks in to determine when/what to render. The PersistGate shouldn't render any child components until the store has been rehydrated. Set a breakpoint in the render method and verify that it's not being called until the rehydrate action has fired. If it's rendering after the store has been rehydrated, verify your containers/selectors, etc are setting the props appropriately for your component.

      – SomethingOn
      Mar 4 at 15:16















    13





    +400









    The persistStore function which is used to make your store persistent have a third param callback which is invoked after store rehydration is done. You have to start your app with some sort of preloader, which waits for rehydration to happen and renders your full application only after it finishes.



    redux-persist docs even have a recipe for this scenario. In your case all the react-router stuff should be rendered inside initial loader as well:



    export default class Preloader extends Component 

    constructor()
    super()
    this.state = rehydrated: false


    componentWillMount()
    persistStore(this.props.store, , () =>
    this.setState( rehydrated: true );
    )


    render()
    if(!this.state.rehydrated)
    return <Loader />;

    return (
    <Provider store=store>
    <ConnectedRouter history=history>
    <App />
    </ConnectedRouter>
    </Provider>
    );



    const store = ...; // creating the store but not calling persistStore yet
    ReactDOM.render(<Preloader store=store />, ... );





    share|improve this answer























    • There is also the PersistGate component now that makes delaying rendering until rehydrated even easier and more logical: github.com/rt2zz/redux-persist

      – SomethingOn
      Aug 22 '18 at 15:36











    • @SomethingOn doesn't help it seems. Have the same issue with PersistGate even.

      – Petros Kyriakou
      Feb 28 at 6:49











    • @PetrosKyriakou sounds like you may need to add some checks in to determine when/what to render. The PersistGate shouldn't render any child components until the store has been rehydrated. Set a breakpoint in the render method and verify that it's not being called until the rehydrate action has fired. If it's rendering after the store has been rehydrated, verify your containers/selectors, etc are setting the props appropriately for your component.

      – SomethingOn
      Mar 4 at 15:16













    13





    +400







    13





    +400



    13




    +400





    The persistStore function which is used to make your store persistent have a third param callback which is invoked after store rehydration is done. You have to start your app with some sort of preloader, which waits for rehydration to happen and renders your full application only after it finishes.



    redux-persist docs even have a recipe for this scenario. In your case all the react-router stuff should be rendered inside initial loader as well:



    export default class Preloader extends Component 

    constructor()
    super()
    this.state = rehydrated: false


    componentWillMount()
    persistStore(this.props.store, , () =>
    this.setState( rehydrated: true );
    )


    render()
    if(!this.state.rehydrated)
    return <Loader />;

    return (
    <Provider store=store>
    <ConnectedRouter history=history>
    <App />
    </ConnectedRouter>
    </Provider>
    );



    const store = ...; // creating the store but not calling persistStore yet
    ReactDOM.render(<Preloader store=store />, ... );





    share|improve this answer













    The persistStore function which is used to make your store persistent have a third param callback which is invoked after store rehydration is done. You have to start your app with some sort of preloader, which waits for rehydration to happen and renders your full application only after it finishes.



    redux-persist docs even have a recipe for this scenario. In your case all the react-router stuff should be rendered inside initial loader as well:



    export default class Preloader extends Component 

    constructor()
    super()
    this.state = rehydrated: false


    componentWillMount()
    persistStore(this.props.store, , () =>
    this.setState( rehydrated: true );
    )


    render()
    if(!this.state.rehydrated)
    return <Loader />;

    return (
    <Provider store=store>
    <ConnectedRouter history=history>
    <App />
    </ConnectedRouter>
    </Provider>
    );



    const store = ...; // creating the store but not calling persistStore yet
    ReactDOM.render(<Preloader store=store />, ... );






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Apr 5 '17 at 4:01









    fkulikovfkulikov

    2,274822




    2,274822












    • There is also the PersistGate component now that makes delaying rendering until rehydrated even easier and more logical: github.com/rt2zz/redux-persist

      – SomethingOn
      Aug 22 '18 at 15:36











    • @SomethingOn doesn't help it seems. Have the same issue with PersistGate even.

      – Petros Kyriakou
      Feb 28 at 6:49











    • @PetrosKyriakou sounds like you may need to add some checks in to determine when/what to render. The PersistGate shouldn't render any child components until the store has been rehydrated. Set a breakpoint in the render method and verify that it's not being called until the rehydrate action has fired. If it's rendering after the store has been rehydrated, verify your containers/selectors, etc are setting the props appropriately for your component.

      – SomethingOn
      Mar 4 at 15:16

















    • There is also the PersistGate component now that makes delaying rendering until rehydrated even easier and more logical: github.com/rt2zz/redux-persist

      – SomethingOn
      Aug 22 '18 at 15:36











    • @SomethingOn doesn't help it seems. Have the same issue with PersistGate even.

      – Petros Kyriakou
      Feb 28 at 6:49











    • @PetrosKyriakou sounds like you may need to add some checks in to determine when/what to render. The PersistGate shouldn't render any child components until the store has been rehydrated. Set a breakpoint in the render method and verify that it's not being called until the rehydrate action has fired. If it's rendering after the store has been rehydrated, verify your containers/selectors, etc are setting the props appropriately for your component.

      – SomethingOn
      Mar 4 at 15:16
















    There is also the PersistGate component now that makes delaying rendering until rehydrated even easier and more logical: github.com/rt2zz/redux-persist

    – SomethingOn
    Aug 22 '18 at 15:36





    There is also the PersistGate component now that makes delaying rendering until rehydrated even easier and more logical: github.com/rt2zz/redux-persist

    – SomethingOn
    Aug 22 '18 at 15:36













    @SomethingOn doesn't help it seems. Have the same issue with PersistGate even.

    – Petros Kyriakou
    Feb 28 at 6:49





    @SomethingOn doesn't help it seems. Have the same issue with PersistGate even.

    – Petros Kyriakou
    Feb 28 at 6:49













    @PetrosKyriakou sounds like you may need to add some checks in to determine when/what to render. The PersistGate shouldn't render any child components until the store has been rehydrated. Set a breakpoint in the render method and verify that it's not being called until the rehydrate action has fired. If it's rendering after the store has been rehydrated, verify your containers/selectors, etc are setting the props appropriately for your component.

    – SomethingOn
    Mar 4 at 15:16





    @PetrosKyriakou sounds like you may need to add some checks in to determine when/what to render. The PersistGate shouldn't render any child components until the store has been rehydrated. Set a breakpoint in the render method and verify that it's not being called until the rehydrate action has fired. If it's rendering after the store has been rehydrated, verify your containers/selectors, etc are setting the props appropriately for your component.

    – SomethingOn
    Mar 4 at 15:16













    0














    When user refresh the page, The first LOCATION_CHANGE will fire because you sync history with store. However, it only syncs the history and does not redirect user to anywhere yet.



    The only thing matters is the second LOCATION_CHANGE, which should occur after persistStore(). Good news is persistStore() has a call back.



    const store = ...

    // persist store
    persistStore(store, options, () =>
    const states = store.getState();

    // if authenticated, do nothing,
    // if not, redirect to login page
    if (!states.auth.isAuthenticated)
    // redirect to login page

    );

    render(...);


    Hope this can help to solve your problem.






    share|improve this answer





























      0














      When user refresh the page, The first LOCATION_CHANGE will fire because you sync history with store. However, it only syncs the history and does not redirect user to anywhere yet.



      The only thing matters is the second LOCATION_CHANGE, which should occur after persistStore(). Good news is persistStore() has a call back.



      const store = ...

      // persist store
      persistStore(store, options, () =>
      const states = store.getState();

      // if authenticated, do nothing,
      // if not, redirect to login page
      if (!states.auth.isAuthenticated)
      // redirect to login page

      );

      render(...);


      Hope this can help to solve your problem.






      share|improve this answer



























        0












        0








        0







        When user refresh the page, The first LOCATION_CHANGE will fire because you sync history with store. However, it only syncs the history and does not redirect user to anywhere yet.



        The only thing matters is the second LOCATION_CHANGE, which should occur after persistStore(). Good news is persistStore() has a call back.



        const store = ...

        // persist store
        persistStore(store, options, () =>
        const states = store.getState();

        // if authenticated, do nothing,
        // if not, redirect to login page
        if (!states.auth.isAuthenticated)
        // redirect to login page

        );

        render(...);


        Hope this can help to solve your problem.






        share|improve this answer















        When user refresh the page, The first LOCATION_CHANGE will fire because you sync history with store. However, it only syncs the history and does not redirect user to anywhere yet.



        The only thing matters is the second LOCATION_CHANGE, which should occur after persistStore(). Good news is persistStore() has a call back.



        const store = ...

        // persist store
        persistStore(store, options, () =>
        const states = store.getState();

        // if authenticated, do nothing,
        // if not, redirect to login page
        if (!states.auth.isAuthenticated)
        // redirect to login page

        );

        render(...);


        Hope this can help to solve your problem.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 5 '17 at 16:25

























        answered Apr 5 '17 at 16:19









        haipham23haipham23

        2762617




        2762617



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43168801%2fredux-persist-rehydrate-previous-auth-state-too-late%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript